简体   繁体   English

未定义对Class :: Function()C ++的引用

[英]undefined reference to Class::Function() C++

After searching for a solution to this for about a half an hour I have made no progress. 在寻找解决方案大约半小时后,我没有任何进展。 The errors are as follows: 错误如下:

s\\My Workspace\\Project\\main.cpp - Line 7 - undefined reference to 'Sally::Sally()' s \\ My Workspace \\ Project \\ main.cpp-第7行-对'Sally :: Sally()'的未定义引用

s\\My Workspace\\Project\\main.cpp - Line 9 - undefined reference to 'Sally::printCrap()' s \\ My Workspace \\ Project \\ main.cpp-第9行-对'Sally :: printCrap()'的未定义引用

main.cpp main.cpp

#include <iostream>
using namespace std;
#include "Sally.h"

int main()
{
    Sally sallyObject;
    sallyObject.printCrap();
}

Sally.h 萨莉·h

#ifndef SALLY_H
#define SALLY_H


class Sally
{
    public:
        Sally();
        void printCrap();
};

#endif // SALLY_H

Sally.cpp 萨莉

#include "Sally.h"
#include <iostream>
using namespace std;

Sally::Sally(){
}

void Sally::printCrap(){
    cout << "Did someone say steak?" << endl;
}

Thank you in advance! 先感谢您!

I know this is a pretty old question but maybe it could help someone. 我知道这是一个很老的问题,但也许可以帮助某人。

So, when adding any additional files (headers, source, etc.) follow this (if using Eclipse or similar IDE): 因此,在添加任何其他文件(标头,源等)时,请遵循以下步骤(如果使用Eclipse或类似的IDE):

New file -> File... -> C/C++ header (source, etc.) -> next, next -> give it a name and make sure it's in the same path with your project, then check "Add file to active project", in build target(s): check all -> Finish. 新文件->文件...-> C / C ++标头(源等)->下一步,下一步->给它命名,并确保它与您的项目位于同一路径,然后选中“将文件添加到活动目录”项目”,在构建目标中:检查所有->完成。

Hope it helps. 希望能帮助到你。

Your linker doesn't find Sally.cpp. 您的链接器找不到Sally.cpp。 ( Quick intro to linker ) 快速介绍链接器

To compile your code type: 编译代码类型:

g++ -o main main.cpp Sally.cpp

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

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