syntax error : identifier 'string'
-
ahoi alle beisammen
also ich wollte mir eine conainerklasse bastel für string. hab versucht alles schön zu machen. danke das design is halt standart
leider bekomme ich beim kompilieren fehler bzgl string. denke er meint damit er kennt den indentifier 'string' nicht. ich habe jedoch string included.Header:
#ifndef STRINGLIST_H #define STRINGLIST_H #include <string> class StringList { public: StringList(void); StringList(const StringList&); ~StringList(void); void AddLine(string); //<<== Hier meckert der Kompiler void DelLine(int); int Length(void); string& operator[](int); string& operator=(const StringList&); private: string *list; int length; }; #endifImplementation:
#include <cassert> #include "StringList.h" StringList::StringList() { list = NULL; length = 0; } StringList::StringList(cons StringList) { StringList::~StringList() { if (list) delete []list; } void StringList::AddLine(string newline) { string *temp = list; list = new string[length+1]; for (int i = 0; i < list.length; i++) list[i] = temp[i]; list[length] = newline; length = length+1; delete []temp; } void StringList::DelLine(int line) { assert(index >= 0 && index < length); string *temp = list, *tmp = list; list = new string[length-1]; int j = 0; for (int i = 0; i < length-1; i++) { if (i == line) j++; else list[i] = tmp[j++]; } length = length-1; delete temp; } int StringList::Length(void) { return length; } string& StringList::operator [](int index) { assert(index >= 0 && index < length); return list[index]; } string& StringList::operator =(const StringList& source) { if (this != &source) { delete list; length = source.Length(); list = new string[length]; for (int i = 0; i < length; i++) list[i] = source[i]; } return *this; }Fehler:
--------------------Configuration: test - Win32 Debug-------------------- Compiling... main.cpp c:\programme\microsoft visual studio\myprojects\parser\test\stringlist.h(13) : error C2061: syntax error : identifier 'string' c:\programme\microsoft visual studio\myprojects\parser\test\stringlist.h(18) : error C2143: syntax error : missing ';' before '&' c:\programme\microsoft visual studio\myprojects\parser\test\stringlist.h(18) : error C2501: 'string' : missing storage-class or type specifiers c:\programme\microsoft visual studio\myprojects\parser\test\stringlist.h(18) : error C2501: '[]' : missing storage-class or type specifiers c:\programme\microsoft visual studio\myprojects\parser\test\stringlist.h(19) : error C2143: syntax error : missing ';' before '&' c:\programme\microsoft visual studio\myprojects\parser\test\stringlist.h(19) : error C2501: 'string' : missing storage-class or type specifiers c:\programme\microsoft visual studio\myprojects\parser\test\stringlist.h(19) : error C2501: '=' : missing storage-class or type specifiers c:\programme\microsoft visual studio\myprojects\parser\test\stringlist.h(22) : error C2143: syntax error : missing ';' before '*' c:\programme\microsoft visual studio\myprojects\parser\test\stringlist.h(22) : error C2501: 'string' : missing storage-class or type specifiers c:\programme\microsoft visual studio\myprojects\parser\test\stringlist.h(22) : error C2501: 'list' : missing storage-class or type specifiers C:\Programme\Microsoft Visual Studio\MyProjects\Parser\test\main.cpp(158) : error C2660: 'AddLine' : function does not take 1 parameters C:\Programme\Microsoft Visual Studio\MyProjects\Parser\test\main.cpp(166) : error C2660: 'AddLine' : function does not take 1 parameters C:\Programme\Microsoft Visual Studio\MyProjects\Parser\test\main.cpp(170) : error C2248: 'length' : cannot access private member declared in class 'StringList' c:\programme\microsoft visual studio\myprojects\parser\test\stringlist.h(23) : see declaration of 'length' C:\Programme\Microsoft Visual Studio\MyProjects\Parser\test\main.cpp(172) : error C2664: 'DelPreWhitespace' : cannot convert parameter 1 from 'int' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' No constructor could take the source type, or constructor overload resolution was ambiguous Error executing cl.exe. test.exe - 14 error(s), 0 warning(s)es geht mir prinzipel erstmal nur um den ersten fehler, da ich denke, dass das problem, das dahinter steckt, das grundproblem ist.
danke jetzt schon mal.MamboKurt
-
string ist im Namespace std, also bitte std::string im Header und using namespace std; an den Anfang der Implementierung.
-
danke
hab schon lange nichts mehr damit gemacht.
das funktioniert jetzt.jetzt meckert er wegen unresolved external symbol:
--------------------Configuration: test - Win32 Debug-------------------- Linking... main.obj : error LNK2001: unresolved external symbol "public: __thiscall StringList::~StringList(void)" (??1StringList@@QAE@XZ) main.obj : error LNK2001: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall StringList::operator[](int)" (??AStringList@@QAEAAV?$basic_string@DU?$char_traits@D@st d@@V?$allocator@D@2@@std@@H@Z) main.obj : error LNK2001: unresolved external symbol "public: int __thiscall StringList::Length(void)" (?Length@StringList@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: void __thiscall StringList::AddLine(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?AddLine@StringList@@QAEXV?$basic_string@DU?$char_traits@D@s td@@V?$allocator@D@2@@std@@@Z) main.obj : error LNK2001: unresolved external symbol "public: __thiscall StringList::StringList(void)" (??0StringList@@QAE@XZ) Debug/test.exe : fatal error LNK1120: 5 unresolved externals Error executing link.exe. test.exe - 6 error(s), 0 warning(s)in der main.cpp hab ich
#include "StringList.h"in der StringList.cpp hab ich auch
#include "StringList.h"is was falsch dran?
-
Du musst dem Linker sagen, er soll gefälligst die Objektdatei zu StringList.cpp dazulinken. Mit dem Programmcode hat das nichts zu tun.
-
MamboKurt schrieb:
also ich wollte mir eine conainerklasse bastel für string.
typedef std::list<std::string> StringList; // wobei du, deinem Code nach zu urteilen, eher folgendes suchst: typedef std::vector<std::string> StringVector;
-
ja dass es sowas schon gibt is mir schon klar, aber ich will mir die halt mal selber basteln um mal wieder in form zu kommen.
also das hat sich erledigt. funktioniert jetzt alles. hab vergessen die dateien dem projekt hinzuzufügen.MamboKurt