简体   繁体   中英

C++ Undefined symbols for architecture x86_64

I am new to c++ and was trying to save text into a text file. However, when I run the program, I keep getting this error from eclipse and could not figure out what went wrong:

 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

Here are my codes:

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 "";
}

I think you should turn

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

into

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

You are using execute as member function and declared it, but is implemented as free function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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