Search code examples
c++stringcompiler-errorsintidentifier

C++ error C4430 int assumed


I'm a newbie in C++ and have just a small header file in C++ with a simple struct in it.

PGNFinder.h:

#ifndef PGNFINDER_H
#define PGNFINDER_H

struct Field
{
    int Order;
    string Name;
   //more variables but doesn't matter for now
};

#endif

This gives the next errors:

error C2146: syntax error : missing ';' before identifier 'Name'    
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

when I change it to:

   struct Field
{
    int Order;
    std::string Name;
};

It gives a error in the .exe file and the .obj file

error LNK1120: 1 unresolved externals   (in the .exe file)
error LNK2019: unresolved external symbol "int __cdecl Convert::stringToInt(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?stringToInt@Convert@@YAHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "private: void __thiscall CAN::calculateMessageLength(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?calculateMessageLength@CAN@@AAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

and when I add

#include <string> 

and change back to

string Name;

It gives the same errors as in the beginning. So why can't the header file recognize the int and string?

Thanks for the help :)


Solution

  • In order to use string as type of a variable, you need to

    • include the header in which it is declared (#include <string>)
    • use a full qualified type such as std::string or by means of the using directory using namespace std; Note, however, that using is not recommended in header files (see "using namespace" in c++ headers)

    If you only try one of these, it won't work.

    However, your second error message seems to point to a linker problem.