【发布时间】:2015-07-09 17:25:03
【问题描述】:
我正在尝试在 dev c++ 中使用我自己定义的头文件运行我的代码。
但是当我编译它时得到这个错误:
25 C:\Users\ali\Desktop\hw2\Makefile.win recipe for target 'tar.exe' failed
它指向makefile.win中的这一行:
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
而且我已经将我的文件添加到我的项目中: main.cpp&post.cpp&post.h
这是我的代码(仅限 PostBox 类):
在头文件中:
#include <iostream>
#include <string>
using namespace std;
#ifndef P
#define P
class PostBox{
friend void Send(PostBox);
public:
int getcount();
void Count();
PostBox(string,float,int,Person,Person);
PostBox();
void setID(string);
void setweight(float);
void setsender(Person);
void setreceiver(Person);
void cost();
string getid();
Person getsender();
Person getreceiver();
protected:
static int count;
string id;
float weight;
int price;
Person sender;
Person receiver;
};
#endif
在我的 cpp 文件中:
#include <iostream>
#include "post.h"
using namespace std;
PostBox::PostBox(string id,float weight,int price,Person sender,Person receiver)
{
this->count=0;
this->id=id;
this->weight=weight;
this->price=price;
this->sender=sender;
this->receiver=receiver;
}
【问题讨论】:
-
链接器没有其他错误信息吗?
-
C:\Users\ali\Desktop\hw2\post.o post.cpp:(.rdata$.refptr._ZN7PostBox5countE[.refptr._ZN7PostBox5countE]+0x0):未定义对`PostBox的引用: :count'
-
所以你的makefile没有问题。你检查过,你已经实现了
PostBox::count的定义吗?我猜您缺少静态类变量的定义。 -
count 是一个统计变量...不是函数
-
是的,我说过,你需要定义你在标题中声明的静态类变量。见这里:stackoverflow.com/questions/23767241/…