C++体系结构x86_64的未定义符号

C++ Undefined symbols for architecture x86_64

本文关键字:未定义 符号 体系结构 x86 C++      更新时间:2023-10-16

我是c++的新手,正试图将文本保存到文本文件中。然而,当我运行该程序时,我一直从eclipse中得到这个错误,并且无法弄清楚出了什么问题:

 Undefined symbols for architecture x86_64:
 "StorageSave::execute(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
 _main in SpendTracker.o

这是我的代码:

StorageSave.h:

#include <fstream>
#include <string>
#include <vector>
#include "Account/Account.h"
#ifndef STORAGE_STORAGESAVE_H_
#define STORAGE_STORAGESAVE_H_
class StorageSave {
public:
    StorageSave();
    virtual ~StorageSave();
    std::string execute(std::string allData);
};
#endif /* STORAGE_STORAGESAVE_H_ */

StorageSave.cpp:

#include <iostream>
#include <fstream>
#include "StorageSave.h"
StorageSave::StorageSave() {
    // TODO Auto-generated constructor stub
}
StorageSave::~StorageSave() {
    // TODO Auto-generated destructor stub
}
std::string execute(std::string allData) {
    std::ofstream file("hey.txt");
    //std::ofstream *fileptr = &file;
    if(!file.is_open()) {
        std::cout << "Unable to open file." << std::endl;
    } else {
        std::string textData = "test";
        file << textData;
        file.close();
    }
    return "";
}

我认为你应该打开

std::string execute(std::string allData) {

进入

std::string StorageSave::execute(std::string allData) {

您使用execute作为成员函数并声明了它,但它被实现为自由函数