简体   繁体   English

无法调用其他类-ERR-未解析的外部符号

[英]Cannot Call Another Class - ERR - Unresolved External Symbol

I am trying to call another function, foo(), in the Raw class from main.cpp but I keep on getting this error and I do not understand what is wrong with my code. 我正在尝试从main.cpp调用Raw类中的另一个函数foo(),但我不断收到此错误,而且我不明白我的代码有什么问题。 I am working in C++, and I am using the QT framework. 我正在使用C ++,并且正在使用QT框架。 I am new to this language and framework. 我是这种语言和框架的新手。

Error: LNK2019: unresolved external symbol "public:void __thiscall RAW::foo(void)" (?foo@Raw@@QAEXXZ) referenced in function_main. 错误:LNK2019:在function_main中引用的未解析的外部符号“ public:void __thiscall RAW :: foo(void)”(?foo @ Raw @@ QAEXXZ)。 File not found:main.obj 找不到文件:main.obj

main.cpp main.cpp

#include "raw.h"

using namespace std;


int main(int, char*)
{
    Raw newRaw;
    newRaw.foo();

    return 0;
}

raw.cpp raw.cpp

#include "raw.h"
#include <iostream>

using namespace std;

void foo()
{
    cout << "hi\n";
}

Raw::Raw()
{
    cout << "raw\n";
}

raw.h 原始文件

#ifndef RAW_H
#define RAW_H

class Raw
{
public:
    Raw();
    void foo();
};

#endif // RAW_H

In raw.cpp you have to define foo like this: 在raw.cpp中,您必须像这样定义foo:

void Raw::foo()
{
    cout << "hi\n";
}

You have to put Raw:: so that the compiler knows that this is the class member function foo and not some other independent function. 您必须放入Raw ::,以便编译器知道这是类成员函数foo,而不是其他一些独立函数。

As Mihai was saying, you can also define it in the Header file (.h/.hpp), but that is considered bad practice if your class method is complex in any way. 就像Mihai所说的那样,您也可以在Header文件(.h / .hpp)中定义它,但是如果您的类方法无论如何都是复杂的,则认为这是不好的做法。

class Raw {
public:
    void foo() {
        cout << "hi\n";
    }
};

Only time you should really ever do this is for extremely simple classes and for methods that are nothing more than getters really. 只有真正真正应该这样做的时候,才是极其简单的类,而且这些方法实际上只是吸气剂。

You should understand the difference between defining and declaring something in C++. 您应该了解在C ++中定义和声明内容之间的区别。

Declaring is to simply make a prototype, for example void doSomething(int); 声明只是简单地制作一个原型,例如void doSomething(int); is a valid declaration as it says that the method doSomething takes an int and returns void. 是有效的声明,因为它说doSomething方法采用int并返回void。

Now, you need to describe what it does. 现在,您需要描述它的作用。 This is the definition when you do void doSomething(int val) { cout << val << endl; } 这是您在void doSomething(int val) { cout << val << endl; } void doSomething(int val) { cout << val << endl; } as you are now describing what to do with that function. void doSomething(int val) { cout << val << endl; }因为你现在描述如何处理该功能。

You can make the definition in the header file, as I showed, or in the source file (.c/.cpp) as Mihai showed (which is best practice). 您可以按照我在头文件中的定义进行定义,也可以在Mihai所示的源文件(.c / .cpp)中进行定义(这是最佳做法)。 You can only make your declarations in the Header file, though. 但是,您只能在Header文件中进行声明。

Ok, this are some alternatives: 好的,这是一些替代方法:

//raw.h

#ifndef RAW_H
#define RAW_H

class Raw
{
public:
    Raw();
    void foo(){cout << "raw\n";}
};

or 要么

//raw.h

#ifndef RAW_H
#define RAW_H

class Raw
{
public:
    Raw();
    void foo();
};

Raw::Raw()
{
    cout << "raw\n";
}

In both cases you won't need the implementation in raw.cpp anymore. 在这两种情况下,您都不再需要raw.cpp中的实现。 But as I said before, my first solution is standard c++ practice. 但是正如我之前所说,我的第一个解决方案是标准的c ++实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM