c++ - C++ 构造函数中体系结构 x86_64 的 GCC undefined symbol

标签 c++ gcc syntax compiler-errors

我刚开始一个新项目,我的类框架没有编译。我收到的编译器错误是:

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中)。

如果有人能告诉我为什么会出现此错误,我将非常高兴。提前致谢! :)

最佳答案

您已声明静态变量但尚未定义它们。你需要添加这个

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

到您的 SQLComm.cpp 文件。

尽管...考虑一下,这可能不是您想要的。您打算声明非静态 成员变量,例如,SQLComm 的每个实例都应该包含这些变量,对吗?在这种情况下,只需删除 static(不要将以上内容添加到您的 .cpp 文件中。

关于c++ - C++ 构造函数中体系结构 x86_64 的 GCC undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19988766/

相关文章:

c++ - UTF-16 codecvt 方面

c++ - 后缀增量运算符

c++ - 将 char[] 转换为 unsigned int 得到 : dereferencing type-punned pointer will break strict-aliasing rules

c++ - 如何使用函数指针作为类中的参数

c++ - 在 Windows 7 上编译和运行 GTK+ 应用程序

scala - 参数 : _* mean in Scala? 是什么意思

javascript - jQuery 链接以创建连接字符串

c++ - C++ 数组上指针数学的未定义行为

c - 当我尝试将数组作为 m*1 大小的特定矩阵情况时出现问题

javascript - ' and "之间有区别吗