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!

Barnett_Love
  • 43
  • 1
  • 1
  • 9

2 Answers2

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() { ...
Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 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? – Barnett_Love Nov 05 '13 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`. – juanchopanza Nov 05 '13 at 10:15
0

add std to string

std::string b;
Stephane Delcroix
  • 16,134
  • 5
  • 57
  • 85
CroCo
  • 5,531
  • 9
  • 56
  • 88