error:"class "structname" has no member "member"".
Mar 1, 2019 at 5:33pm UTC
Hi everyone, I'm using the code listed down there and im getting the following error:"class "CandyBar" has no member "brand"". Even thought VS2017 displays this error it compiles and works as I'd expect it to work but it's annoying and I'd like to fix it. Any ideas? Thanks!:)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
void fillStruct(CandyBar& candy, std::string name = "Millenium Munch" , double mass = 2.85, int cal = 350)
{
candy.brand = name;
candy.weight = mass;
candy.calories = cal;
}
void showStruct(const CandyBar candy)
{
std::cout << candy.brand << " | " << candy.weight << " | " << candy.calories << std::endl;
}
struct CandyBar
{
std::string brand;
double weight;
int calories;
};
int main()
{
CandyBar candy;
fillStruct(candy);
showStruct(candy);
return 0;
}
Mar 1, 2019 at 5:57pm UTC
When the compiler is compiling lines 1-6, it has not seen the definition of CandyBar yet, so it does not know what members CandyBar has.
My copy of VS2017 doesn't compile the code you've shown.
Move lines 13-18 in front of line fillStruct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <string>
#include <iostream>
struct CandyBar
{
std::string brand;
double weight;
int calories;
};
void fillStruct(CandyBar& candy, std::string name = "Millenium Munch" , double mass = 2.85, int cal = 350)
{
candy.brand = name;
candy.weight = mass;
candy.calories = cal;
}
void showStruct(const CandyBar candy)
{
std::cout << candy.brand << " | " << candy.weight << " | " << candy.calories << std::endl;
}
int main()
{
CandyBar candy;
fillStruct(candy);
showStruct(candy);
return 0;
}
Last edited on Mar 1, 2019 at 5:59pm UTC
Mar 1, 2019 at 6:21pm UTC
I've just tried it and it fixed the issue, weird. I could've sworn that this was the first thing I tried. Thank you for your answer, have a nice day =)
Topic archived. No new replies allowed.