提问人:Jacobe 提问时间:10/28/2020 最后编辑:JabberwockyJacobe 更新时间:10/29/2020 访问量:682
创建 Integer 类时 c++ 中的头文件未定义引用
Header file undefined reference in c++ while creating a Integer class
问:
我尝试创建一个 Integer 类,它对我来说看起来没问题,但测试程序说对重载方法的未定义引用。有人可以帮我找出错误吗?非常感谢任何修复此错误和改进代码的建议。
先谢谢你。
以下是我遇到的错误。
/usr/bin/ld: /tmp/ccMork6I.o: in function `main':
testMyInteger.cpp:(.text+0x29): undefined reference to `MyInteger::MyInteger(int)'
/usr/bin/ld: testMyInteger.cpp:(.text+0x3a): undefined reference to `MyInteger::MyInteger(int)'
/usr/bin/ld: testMyInteger.cpp:(.text+0x65): undefined reference to `operator<<(std::ostream&, MyInteger const&)'
/usr/bin/ld: testMyInteger.cpp:(.text+0x9c): undefined reference to `MyInteger::getValue() const'
/usr/bin/ld: testMyInteger.cpp:(.text+0xe0): undefined reference to `operator<<(std::ostream&, MyInteger const&)'
/usr/bin/ld: testMyInteger.cpp:(.text+0x106): undefined reference to `MyInteger::setValue(int)'
/usr/bin/ld: testMyInteger.cpp:(.text+0x119): undefined reference to `MyInteger::operator+(MyInteger const&) const'
/usr/bin/ld: testMyInteger.cpp:(.text+0x141): undefined reference to `operator<<(std::ostream&, MyInteger const&)'
/usr/bin/ld: testMyInteger.cpp:(.text+0x17f): undefined reference to `MyInteger::operator-(MyInteger const&) const'
/usr/bin/ld: testMyInteger.cpp:(.text+0x191): undefined reference to `operator<<(std::ostream&, MyInteger const&)'
/usr/bin/ld: testMyInteger.cpp:(.text+0x1cc): undefined reference to `operator>>(std::istream&, MyInteger&)'
/usr/bin/ld: testMyInteger.cpp:(.text+0x1df): undefined reference to `MyInteger::operator==(MyInteger const&) const'
/usr/bin/ld: testMyInteger.cpp:(.text+0x24e): undefined reference to `MyInteger::operator==(MyInteger const&) const'
/usr/bin/ld: testMyInteger.cpp:(.text+0x2b5): undefined reference to `MyInteger::setValue(int)'
/usr/bin/ld: testMyInteger.cpp:(.text+0x2da): undefined reference to `operator<<(std::ostream&, MyInteger const&)'
/usr/bin/ld: testMyInteger.cpp:(.text+0x314): undefined reference to `operator<<(std::ostream&, MyInteger const&)'
collect2: error: ld returned 1 exit status
下面是 MyInteger.h
#ifndef MYINTEGER_H
#define MYINTEGER_H
#include <iostream>
using namespace std;
class MyInteger
{
public:
MyInteger(int v = 0);
void setValue(int v);
int getValue() const;
MyInteger operator+(const MyInteger &r) const;
MyInteger operator-(const MyInteger &r) const;
bool operator==(const MyInteger &r) const;
friend ostream &operator<<(ostream &out, const MyInteger &r);
friend istream &operator>>(istream &in, MyInteger &r);
private:
int value;
};
#endif
下面是 MyInteger.cpp 文件
#include "MyInteger.h"
#include <iostream>
using namespace std;
// constructer
// initiate the value with v if given else 0
MyInteger::MyInteger(int v = 0){
value = v;
}
// method to set the value
void MyInteger::setValue(int v){
value = v;
}
// method to return the value
int MyInteger::getValue() const {
return value;
}
// overloading + operator
MyInteger MyInteger::operator+(const MyInteger &r) const {
MyInteger res;
res.setValue(res.getValue() + r.getValue());
return res;
}
// overloading - operator
MyInteger MyInteger::operator-(const MyInteger &r) const {
MyInteger res;
res.setValue(res.getValue() - r.getValue());
return res;
}
// overloading comparison operator (==)
bool MyInteger::operator==(const MyInteger &r) const {
MyInteger res;
return res.getValue() == r.getValue();
}
ostream &operator<<(ostream &out, const MyInteger &r){
out << r.getValue();
return out;
}
istream &operator>>(istream &in, MyInteger &r){
int tmp;
in >> tmp;
r.setValue(tmp);
return in;
}
以下是我的驱动程序代码
#include <iostream>
#include "MyInteger.h"
using namespace std;
int main()
{
MyInteger i1; // 0
MyInteger i2(5); // 5
MyInteger i3 = i2; // 5
cout << "i1: " << i1 << endl;
cout << "i2: " << i2.getValue() << endl;
cout << "i3: " << i3 << endl;
i1.setValue(-4);
i3 = i1 + i2;
cout << "i3: " << i3 << endl; // 1
cout << "i2 - i1: " << i2 - i1 << endl; // 9
cout << "Enter an integer: ";
cin >> i1; // enter 123
if (i1 == i2) // different
cout << "same" << endl;
else
cout << "different" << endl;
i2 = i1;
if (i1 == i2) // same
cout << "same" << endl;
else
cout << "different" << endl;
i2.setValue(25);
cout << "i1: " << i1 << endl; // 123
cout << "i2: " << i2 << endl; // 25
return 0;
}
答:
3赞
lubgr
10/28/2020
#1
首先,由于 的构造函数的定义,代码无法编译。默认参数在声明和定义中都给出,这是无效的。只需在实现中删除它,您应该没问题:MyInteger
= 0
MyInteger::MyInteger(int v){
value = v;
}
其次,在从文件增量构建对象文件后,您需要将所有内容链接在一起。例:.cpp
g++ -o MyInteger.o -c MyInteger.cpp
g++ -o main.o -c main.cpp
g++ -o test-exec main.o MyInteger.o
评论
0赞
lubgr
10/28/2020
当然,只需发布一个新问题,因为这是单独的问题。
2赞
n314159
10/28/2020
#2
似乎您只使用 -file 而不是 -file 进行编译。也就是说,您对编译器的调用是这样的:main.cpp
MyInteger.cpp
g++ main.cpp
它应该像g++ main.cpp MyInteger.cpp
评论
but the test program says undefined reference to the overloaded methods.
请添加完整的错误消息。