quot;undefined reference toquot; in G++ Cpp(“未定义的参考在 G++ 中)
问题描述
似乎无法消除错误.错误如下.我在谷歌上看过,仍然无法弄清楚.好像我不是 Cpp 的新手,但有一段时间没有被它愚弄过.
Can't seem to get the errors to go away. Errors are below. I have looked on google and still can't figure it out. It is not like I am new to Cpp, but have not fooled with it in a while.
奇怪的是它可以在 Windows 中使用 G++...
错误:
- [ze@fed0r!---**__*]$ g++ main.cpp
 - /tmp/ccJL2ZHE.o:在函数'main'中:
 - main.cpp:(.text+0x11): 对 `Help::Help()' 的未定义引用
 - main.cpp:(.text+0x1d): 对 `Help::sayName()' 的未定义引用
 - main.cpp:(.text+0x2e): 对 `Help::~Help()' 的未定义引用
 - main.cpp:(.text+0x46): 对 `Help::~Help()' 的未定义引用
 - collect2: ld 返回 1 个退出状态
 
- [ze@fed0r! ---**__*]$ g++ main.cpp
 - /tmp/ccJL2ZHE.o: In function `main':
 - main.cpp:(.text+0x11): undefined reference to `Help::Help()'
 - main.cpp:(.text+0x1d): undefined reference to `Help::sayName()'
 - main.cpp:(.text+0x2e): undefined reference to `Help::~Help()'
 - main.cpp:(.text+0x46): undefined reference to `Help::~Help()'
 - collect2: ld returned 1 exit status
 
main.cpp
#include <iostream>
#include "Help.h"
using namespace std;
int main () {
    Help h;
    h.sayName();
    // ***
    // ***
    // ***
    return 0;
}
帮助.h
#ifndef HELP_H
#define HELP_H
class Help {
    public:
        Help();
        ~Help();
        void sayName();
    protected:
    private:
};
#endif // HELP_H
帮助.cpp
#include <iostream>
#include "Help.h"
using namespace std;
Help::Help() { // Constructor
}
Help::~Help() { // Destructor
}
void Help::sayName() {
    cout << "            ***************" << endl;
    cout << "   ************************************" << endl;
    cout << "              ************" << endl;
    cout << "         *********************" << endl;
}
推荐答案
g++ main.cpp Help.cpp
g++ main.cpp Help.cpp
你必须告诉编译器你希望它编译的所有文件,而不仅仅是第一个.
You have to tell the compiler all the files that you want it to compile, not just the first one.
这篇关于“未定义的参考"在 G++ 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“未定义的参考"在 G++ 中
				
        
 
            
        基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
 - 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
 - 如果我为无符号变量分配负值会发生什么? 2022-01-01
 - 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
 - 初始化列表*参数*评估顺序 2021-01-01
 - CString 到 char* 2021-01-01
 - GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
 - 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
 - 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
 - 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				