创建 Integer class 时,C++ 中的头文件未定义引用

Header file undefined reference in c++ while creating a Integer class

我尝试创建一个 Integer class,它对我来说看起来没问题,但测试程序说未定义对重载方法的引用。有人可以帮我找出错误。非常感谢任何修复此错误和改进代码的建议。

提前致谢。

以下是我遇到的错误。

/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;
}

首先,由于 MyInteger 的构造函数定义,代码无法编译。默认参数 = 0 在声明和定义中都给出,这是无效的。只需在实现中将其删除,就可以了:

MyInteger::MyInteger(int v){
    value = v;
}

其次,在从 .cpp 文件逐步构建目标文件后,您需要 link 将所有内容整合在一起。示例:

g++ -o MyInteger.o -c MyInteger.cpp
g++ -o main.o -c main.cpp

g++ -o test-exec main.o MyInteger.o

您似乎只使用 main.cpp 文件而不是 MyInteger.cpp 文件进行编译。 IE。您对编译器的调用类似于: g++ main.cpp

应该是这样的 g++ main.cpp MyInteger.cpp