2013-11-14 189 views
0

我刚启动了一个新项目,并且我的类骨架不能编译。我收到编译器错误是:GCC未定义的C++架构x86_64符号构造函数

Undefined symbols for architecture x86_64: 
    "SQLComm::ip", referenced from: 
     SQLComm::SQLComm(int, std::__1::basic_string<char, std::__1::char_traits<char>,  std::__1::allocator<char> >) in SQLComm.o 
    "SQLComm::port", referenced from: 
    SQLComm::SQLComm(int, std::__1::basic_string<char, std::__1::char_traits<char>,  std::__1::allocator<char> >) in SQLComm.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

我不知道为什么我的代码不编译......这里的类错误:

SQLComm.h:

#ifndef __WhisperServer__SQLComm__ 
#define __WhisperServer__SQLComm__ 

#include <iostream> 
#include <string> 

class SQLComm { 
public: 
//Local vars 
static int port; 
static std::string ip; 

//Public functions 
void connect(); 
SQLComm(int sqlport, std::string sqlip); 
~SQLComm(); 
private: 

}; 



#endif /* defined(__WhisperServer__SQLComm__) */ 

而且这里是SQLComm.cpp:

#include "SQLComm.h" 


SQLComm::SQLComm(int sqlport, std::string sqlip){ 
ip = sqlip; 
port = sqlport; 
} 

SQLComm::~SQLComm(){ 

} 

void SQLComm::connect(){ 

} 

系统是OSX10.9,编译器是GCC(在xCode中)。

如果有人能告诉我为什么我得到这个错误,我会很高兴。提前致谢! :)

+0

我不知道为什么当我发布这个缩进,但由于程序非常短会让它保持这种状态 –

回答

1

你已经声明了静态变量,但是你没有定义它们。您需要将此

int SQLComm::port; 
std::string SQLComm::ip; 

添加到您的SQLComm.cpp文件中。

虽然......想想这可能是而不是你想要的。您打算声明非静态成员变量,例如,SQLComm的每个实例都应包含这些变量,对吗?在这种情况下,简单地丢弃static(不上面添加到您的.cpp文件。

+0

辉煌,谢谢!来自Java和C我习惯于Java的最终和C风格的静态。再次感谢:) –

1

您需要定义您的静态类变量。在SQLComm.cpp尝试

int SQLComm::port; 
std::string SQLComm::ip; 

注意:大多数情况下,你不想将这两个变量声明为静态类变量,而是作为普通实例变量声明。

相关问题