对外部类的未定义引用

Undefined reference to an external class

本文关键字:引用 未定义 对外部      更新时间:2023-10-16
C:UsersPCDesktoprandommain.o:main.cpp:(.text+0x76)||undefined reference to `Tclass::FFunction()'|

我创建了自己的类,该类位于主程序的外部,这是我遇到的错误。这是我的程序的代码。

主程序(.cpp)

#include<iostream>
#include "Tclass.h"
#include "Tclass.cpp"
using namespace std;
int main(){
    Tclass object;
    object.FFunction();
    return 0;
}

头文件。(.h)

#ifndef TCLASS_H
#define TCLASS_H

class Tclass
{
    public:
        Tclass();
        void FFunction();
};
#endif // TCLASS_H

C ++样式表(我认为这就是它的名字)(.cpp)

#include "Tclass.h"
#include<iostream>
using namespace std;
Tclass::Tclass()
{
    cout << "An object for this class has been created n";
}
void FFunction(){
    cout << "The function has been created n";
}

我正在使用代码::块作为我的IDE。我还创建了带有任何析构函数的类

在您的

.cpp文件中:

void Tclass::FFunction(){
    cout << "The function has been created n";
}

而不是:

void FFunction(){
    cout << "The function has been created n";
}

此外,无需将Tclass.cpp包含在您的主目录中。