0

practice.h

struct CandyBar
{
    string name;
    double weight;
    int calories;

};

practice.cpp

#include    <iostream>
#include    <string>
#include    "practice.h"  

using namespace std;

int main()
{
    CandyBar snacks{ "Mocha Munch", 2.3, 350 };

    cout << snacks.name << "\t" << snacks.weight << "\t" << snacks.calories << endl;
    return 0;
}

when I build the solution, I get the errors:

practice.h(5): error C2146: syntax error : missing ';' before identifier 'name'

practice.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2440: 'initializing' : cannot convert from 'const char [12]' to 'double'

There is no context in which this conversion is possible

practice.cpp(20): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data

practice.cpp(20): error C2078: too many initializers

practice.cpp(22): error C2039: 'name' : is not a member of 'CandyBar'

practice.h(4) : see declaration of 'CandyBar'

what is cause of all the errors? why won't the variables get recognized as fields of the struct?

2
  • 3
    You're missing #include <string> and std:: before string name; in the header. Edit: Didn't notice that you're including <string> before including the header in your source file, so it should work without the include statement too, but it's still good practice to add the include statement in the header since it does depend on std::string. And using namespace std; is very bad practice! Commented Dec 20, 2013 at 23:18
  • 1
    At least this using namespace std; was in a *.cpp file and not in a header. Commented Dec 20, 2013 at 23:22

4 Answers 4

5

The issue is that when the headers parser there is no type string.

The best way is to include the namespace e.g.

struct CandyBar
{
    std::string name;
    double weight;
    int calories;

};

This does not show up in the cpp file as you have using namespace std;

You could put the using line before the #include "practice.h" but that is considered bad style as the header is now not self contained and you could have namespace conflicts.

Sign up to request clarification or add additional context in comments.

2 Comments

if I have #include <string> and std namepace in the cpp file, why do I have to do std::string?
The using line is currently after the #include "practice.h" so when the compiler reads the practice.h header it does not know that you are using namespace std
1

You need to include in practice.h.

Like so:

#include <string>

struct CandyBar
{
   std::string name;  // And also std:: before string, as Praetorian pointed out  
   double weight;
   int calories;
};

3 Comments

Doesn't matter for this small sample, string is already included at the point where the header is parsed.
@Niels: You're right. I missed that, thanks. That said, I'm keeping it as is, as it is usually better to have the headers included in the files where you're reliant on their definitions to avoid future uses of your classes introducing compilation errors.
I agree, I just said it wasn't the solution for the problem at hand until you edited it to also include the namespace fix :)
1

The include is not required, but you must either import namespace std or fully qualify its usage. So either repeat the using statement or declare name as type std::string.

Comments

0

You should use "#ifndef", "#define". Because maybe header files can call to initialize some times. Therefore you take errors. Look this: C++’ta Seperated Compilation II – Header File Kullanmak

1 Comment

While this is good advice, it doesn't address the problem the OP was having.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.