提问者:小点点

C++编译错误:体系结构x86_64的符号未定义


我很难编译这个。我认为它与静态变量有关,但我不能百分之百确定我在做什么。以下是我一直得到的错误消息:

体系结构X86_64的未定义符号:“Counter::nCounters”,引用自Main.o中的:Counter::Counter(int,int)

Main.o中的Counter::GetnCounters()

counter::counter(int,int)in counter.o

Counter::GetnCounters()在Counter.o中

ld:找不到体系结构x86_64的符号

CLANG:错误:链接器命令失败,退出代码为%1(使用-V查看调用)

下面是头文件:

#ifndef project1_Counter_h
#define project1_Counter_h

class Counter
{
private:
int counter;
int limit;
static int nCounters;

public:
Counter(int, int);
void increment();
void decrement();
int getValue();
static int getNCounters();
};

#endif

下面是。cpp文件:

#include "Counter.h"

Counter::Counter(int a, int b)
{
counter = a;
limit = b;
nCounters++;
}

void Counter::increment()
{
if (counter < limit)
    counter++;
}

void Counter::decrement()
{
if (counter > 0)
    counter--;
}

int Counter::getValue()
{
return counter;
}

int Counter::getNCounters()
{    
return nCounters;
}

而main.cpp只是一个简单的Hello World程序。如有任何帮助,我们将不胜感激。


共1个答案

匿名用户

我相信您需要用一个值来初始化nCounters。

尝试添加

int Counter::nCounters = 0;

类之外的某个地方,或者将其初始化为:

static int nCounters = 0;

取而代之的是。

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|编译|体系结构|x86_64|符号|未定义)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?