1

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 :)

Lutske
  • 117
  • 3
  • 17

3 Answers3

2

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.

Community
  • 1
  • 1
Philipp
  • 11,549
  • 8
  • 66
  • 126
0

Since I tend to use the comment function too often.

Your problem is a missing include, and when you included string.h you still forgot the std-namespace of the "string class".

So either use a using namespace std (for beginners best practice, since most stuff will be most likely std stuff) or declare your string as std::string within your struct.

Najzero
  • 3,164
  • 18
  • 18
0

changing it to std::string clearly fixes the compiler error.

Then you have a linker error which is not related to that line of code. You appear to have a 'Convert' class with a missing implementation of a 'stringToInt' function.

Pete
  • 4,784
  • 26
  • 33
  • Thanks! But it is strange that the linker error appeare when the other error is fixed. – Lutske Dec 14 '12 at 09:37
  • Not strange at all. If the source fails to compile then generally it won't try to link it. Since it doesn't run that step of the build you won't see any errors from it. – Pete Dec 17 '12 at 11:09