【发布时间】:2010-02-04 14:34:46
【问题描述】:
请看下面的程序。为什么我会收到错误消息?
#include <stdlib.h>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
class serverData
{
public:
static int serverTemp;
static int server;
};
int main(int argc, char** argv)
{
string s = "sajad bahmani";
serverData::server = 90 ;
const char * a = s.data();
cout << a[0] << endl;
return (EXIT_SUCCESS);
}
同时,我在尝试链接时收到此错误:
build/Debug/GNU-Linux-x86/main.o: In function `main':
/home/sb23/pr/main.cpp:14: undefined reference to `serverData::server'
collect2: ld returned 1 exit status
【问题讨论】:
-
那是不是 编译错误,就是链接错误
-
使用 s.c_str() 从 std::string 获取“C 风格”字符串,s.data() 不保证为空终止。