0

when i build this project in vs2010, error occurs:

  1. syntax error : missing ';' before identifier 'b'
  2. error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  3. error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  4. error C2065: 'b' : undeclared identifier

    #ifndef _B_H
    #define _B_H
    
    #include <string>
    
    class B
    {
    public:
        B();
        ~B();
        void showfunc();
    
        string b;
    };
    
    #endif
    
    /***************************/
    // B.cpp
    #include <iostream>
    #include <string>
    #include "B.h"
    using namespace std;
    B::B()
    {
    }
    void B::showfunc()
    {
     cout<<b<<endl;
    }
    /**************************************/
    // main.cpp
    #include <iostream>
    // #include "B.h"
    using namespace std;
    void main()
    { 
    }
    

Please help me!

1
  • 5
    string is in the std namespace. You need std::string b; Nov 5, 2013 at 8:33

2 Answers 2

1

string is in the std namespace. You need

std::string b; 

You should also be be careful with using namespace std, even in implementation files.

Also, note that void main() is not one of the standard signatures for main in C++. You need

int main() { ...
2
  • I added using namespace std; at the beginning of the head file B.h, and it works. It's troublesome to add std prefix everytime when using classes in the std namespace. But in fact i have using namespace std; sentence at the head of source file B.cpp. why it doesn't work? Nov 5, 2013 at 10:06
  • @Barnett_Love You should not use using namespace std. It is more trouble than it is worth. You will deeply regret it one day. As to your question, putting that in B.cpp has no effect on B.h. Nov 5, 2013 at 10:15
0

add std to string

std::string b;
1
  • thanks a lot. I added using namespace std; at the beginning of the head file B.h, and it works. Nov 5, 2013 at 9:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.