c++ - undefined reference

标签 c++

<分区>

这是 g++ 的输入和生成的错误消息。

$ g++ main.cpp -o keyLogger
/tmp/ccvwRl3A.o:main.cpp:(.text+0x93): undefined reference to `SaveFeatures::SaveFeatures(std::string)'
/tmp/ccvwRl3A.o:main.cpp:(.text+0x93): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SaveFeatures::SaveFeatures(std::string)'
/tmp/ccvwRl3A.o:main.cpp:(.text+0xbf): undefined reference to `SaveFeatures::save(std::string)'
/tmp/ccvwRl3A.o:main.cpp:(.text+0xbf): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SaveFeatures::save(std::string)'
collect2: error: ld returned 1 exit status

我检查并重新检查了 SaveFeatures 类的 .h 和 .cpp 语法,但未能发现错误。欢迎任何帮助。

main.cpp

#include <string>
#include "SaveFeatures.h"

using namespace std;

int main(){
    string fileName="saveTest.text";
    string saveContent="this is a test";
    SaveFeatures saveFeatures(fileName);
    saveFeatures.save(saveContent); 
}

保存特征.cpp

#include "SaveFeatures.h"
#include <string>
using namespace std;

SaveFeatures::SaveFeatures(string fileName){
    setFileName(fileName);
}

void SaveFeatures::setFileName(string fileName){
    if(fileName!=NULL){
        this.fileName=fileName;
    }
}
bool SaveFeatures::save(string content){
    if(fileName==NULL)return false;
    if (content==NULL)return false;
    FILE *file;
    file=fopen(fileName,"a");
    if(file!=NULL){
        fputs(content,file);
    }
        return true;            
}

string SaveFeatures::getFileName(){
    return fileName;
}

保存特征.h

#ifndef SAVEFEATURES_H
#define SAVEFEATURES_H
#include <string>
using namespace std;

class SaveFeatures{
    public: 
        SaveFeatures(string fileName);      
        void setFileName(string fileName);      
        string getFileName();       
        bool save(string content);      
    private:
        string fileName;
        //need to make a method to determine if the fileName has  file extension    
};
#endif

最佳答案

您需要为您的可执行文件指定所有需要的源文件。

g++ main.cpp SaveFeature.cpp -o keyLogger

关于c++ - undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29338483/

相关文章:

c++ - 精神提升: Invalidate parser from member function

c++ - QwtPlot具有相等的轴和自动缩放

c++ - 字符串类中不区分大小写的 find() 方法

c++ - 加载非标准字体并在 QTextDocument 中显示它们

c++ - 访问 eigen3 中的特征值

c++ - 向下转换指向具有附加功能的派生类的共享指针 - 这安全吗?

c++ - 对两个对应的数组进行排序

c++ - 将 `extern template` 与第三方仅 header 库一起使用

c++ - 在 Xcode 中,我在代码片段中发现了语义问题

c++ - 为什么不 boost 元组运算符 == 和 << 编译?