CodeDoctorJL
CodeDoctorJL

Reputation: 1284

How to fix these errors: C2146 & C4430 (in c++)

So, my teacher gave the class this code, but I'm having trouble running it. The errors I get are C2146 & C4430. I tried messing around by adding includes to header files, but I'm still not sure what the problem is.

I've commented the code below where the errors were indicated by the compiler.

assoc.h file

template<class T> class assoc {
 public:

  struct pair {
    string key;   //error C2146: syntax error: missing ';' before identifier 'key'
    T      value; 
    pair  *next;  


    pair() : key(), value(), next(NULL) {}

    pair( const string& key, T value, pair* next = NULL ) //error c4430: missing type specifier - int assumed. Note: c++ does not suport default - int
      : key(key), value(value), next(next) {}
  };

private:
  pair * table;
}

Upvotes: 0

Views: 577

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318688

The string type is not defined. You need to import it from the std namespace:

#include <string>
using std::string;

Of course you could also import everything from the namespace using using namespace std; but that's usually a bad idea - for thing that are not as widespread as string it's better to use the qualified name (e.g. std::pair). Your code is actually a great example for this, with using namespace std your pair would collide with the one from std.

Upvotes: 2

Related Questions