- It gives so many error, why ?
- How can I fix it ?
My real code ;
int SearchUserName ( string searched, vector<User *> list )开发者_Go百科 {
vector<User *> :: iterator it ;
for ( it = list.begin() ; it < list.end ( ) ; ++it ) {
const string tmp = (*it) -> getUsername() ;
if ( tmp == searched ) return 1 ;
}
return 0 ;
}
// SocialNet :: createUser
void SocialNet :: createUser (const string username, const string name, \
const string surname) throw ( SocialNetException ) {
int tmp = SearchUserName ( username, this -> users ) ;
if( tmp == 0) {
User *tmp = NULL ;
tmp = new User( username, name, surname ) ;
users.push_back ( tmp ) ;
return ;
}
throw SocialNetException (USER_ALREADY_EXISTS, username) ;
}
Finally, I have find, Error:
(.text+0x20): undefined reference to main'
/tmp/cckzaRBZ.o: In function
SearchUserName(std::basic_string, std::allocator >, std::vector >)':
SocialNet.cpp:(.text+0x47): undefined reference to User::getUsername() const'
/tmp/cckzaRBZ.o: In function
SocialNet::createUser(std::basic_string, std::allocator >, std::basic_string, std::allocator >, std::basic_string, std::allocator >)':
SocialNet.cpp:(.text+0x258): undefined reference to `User::User(std::basic_string, std::allocator >, std::basic_string, std::allocator >, std::basic_string, std::allocator >)'
EDIT: in makefile ;
CXX = g++
CXXFLAGS_W = -Werror -Wunused-variable -Wunused-value -Wunused-function \
-Wfloat-equal -Wall
CXXFLAGS_M = -ansi -pedantic-errors
CXXFLAGS = ${CXXFLAGS_M} ${CXFLAGS_W}
all: main
./main
compile ; make filename
You're only compiling one file.
something like
main: foo.o bar.o
$(CXX) -o $@ foo.o bar.o
listing all your object files
Comment all your code. And uncomment line after line to determine which line causing the problem.
I'm feeling lucky.
class people
{
public: // <--- add this line
string getpersonname();
};
I think your linker errors are being caused by the fact that one of your .cpp files is not being compiled into the program.
精彩评论