Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        已关闭6年。
                                                                                            
                
        
我在底部创建一个日志文件对象是.h和.cc

波纹管是我得到的错误,请帮助


  架构x86_64的未定义符号:
  引用了“ LogFile :: log(std :: __ 1 :: basic_string,std :: __ 1 :: allocator>)”
  从:
        textdisplay.o中的TextDisplay :: notify(int,int,Cell :: CellType)
        Floor :: loadFloor(std :: __ 1 :: basic_string,std :: __ 1 :: allocator>,Player *)在
  地板
        floor.o中的Floor :: isStairwaySet()
        floor.o中的Floor :: getCellAtCoord(int,int)
        floor.o中的Floor :: getPossibleSpots(Occupant *,int,int)
        floor.o中的Floor :: getNearbyPlayer(int,int)
        character.o中的Character :: dealDamage(int)
        ...“ LogFile :: log(char const *)”,引用自:
        cc3k.o中的CC3K :: cleanUp()
        CC3K :: startGame(Occupant :: SpecificType,std :: __ 1 :: basic_string,
  cc3k.o中的std :: __ 1 :: allocator>)
        cc3k.o中的CC3K :: step()
        cc3k.o中的CC3K :: playerMove(CC3K :: Command,CC3K :: Direction)
        Floor :: loadFloor(std :: __ 1 :: basic_string,std :: __ 1 :: allocator>,Player *)在
  地板
        Floor.:o中的floor :: notifyStairwayBeingSet(int,int)
        cell.o中的Cell :: removeDeadOccupant()
        ...“引用的LogFile :: dlog(std :: __ 1 :: basic_string,std :: __ 1 :: allocator>)”
  从:
        商人.o“ LogFile :: dlog(char const *)”中的Merchant :: attackStep(),引用自:
        character.o中的Character :: attack(Character&)
        商人.o“ LogFile :: getI()”中的Merchant :: attackStep(),引用自:
        cc3k.o中的CC3K :: cleanUp()
        CC3K :: startGame(Occupant :: SpecificType,std :: __ 1 :: basic_string,
  cc3k.o中的std :: __ 1 :: allocator>)
        cc3k.o中的CC3K :: step()
        cc3k.o中的CC3K :: playerMove(CC3K :: Command,CC3K :: Direction)
        textdisplay.o中的TextDisplay :: notify(int,int,Cell :: CellType)
        Floor :: loadFloor(std :: __ 1 :: basic_string,std :: __ 1 :: allocator>,Player *)在
  地板
        Floor.:o中的floor :: notifyStairwayBeingSet(int,int)
        ... ld:找不到架构x86_64的符号


下面是该错误所涉及的一些代码行

LogFile::getI()->log("Error textdisplay.cc 77: Weird cellType given: " + to_string(cellType));
LogFile::getI()->log("Error floor.cc isStairwaySet() 141: Weird values for stairwayRow: " + to_string(stairwayRow) + " stairwayCol: " + to_string(stairwayCol));


。H

#ifndef __LOGFILE_H__
#define __LOGFILE_H__

#include <string>

class LogFile
{
    static LogFile *singleton;

    LogFile(std::string fileName);
    ~LogFile();

    int logNum;
    std::string fileName;

    static void cleanUp();

    public:

        enum Error {};
        //must be called before use, fileName to log too
        static LogFile *initInstance(std::string fileName);
        static LogFile *getI();

        void log(std::string error);
        void log(const char *error);
        void dlog(std::string error);
        void dlog(const char *error);
        bool hasLoged();
        int getNumLogs();
};

#include "logfile.h"


.cc

#include "logfile.h"
#include <ofstream>
#include <cstdlib>

using namespace std;

static LogFile *LogFile::singleton = NULL;

void LogFile::cleanUp()
{
    delete singleton;
}

LogFile::LogFile(std::string fileName): logNum(0), fileName(fileName){}

LogFile::~LogFile(){}

//must be called before use, fileName to log too
LogFile *LogFile::initInstance(std::string fileName)  //static
{
    if(singleton)
    {
        singleton->log("Error: logfile.cc initInstance() 19: Calling this function more than once is wrong implementation");
        return singleton;
    }else
    {
        singleton = new LogFile(fileName);
#ifdef REPRAND
singleton->log("#define REPRAND");
#endif
#define DEBUG
singleton->log("#define DEBUG");
#endif
        atexit(cleanUp)
        return singleton;
    }
}

LogFile *LogFile::getI() //static
{
    if(!singleton)
    {
        LogFile::getI()->log("Error: logfile.cc getInstance() 32: Calling getInstance() without having first called initInstance()");
    }else
    {
        return singleton;
    }
}

void LogFile::log(std::string error)
{
    ofstream ofs;
    ofs.open(fileName.c_str(), ofstream::out | ofstream::app);

    ofs << error << endl;

    if(!ofs.good)
        LogFile::getI()->log("Error writeToFile() 47: output file stream is not good");

    logNum ++;
}

void LogFile::log(char *error)
{
    log(string(error));
}

void LogFile::dlog(std::string error)
{
#ifdef DEBUG
log(error);
#endif
}

void LogFile::dlog(char *error)
{
    dlog(string(error));
}

bool LogFile::hasLoged()
{
    return (logNum > 0);
}

int LogFile::getNumLogs()
{
    return logNum;
}

最佳答案

void log(std::string error)


没有定义您想要的成员函数。你不得不说:

void LogFile::log(std::string error)


您的定义仅定义了一个名为log的裸函数,该函数有效,因此编译器在链接之前不会抱怨。

关于c++ - c++ LogFile对象编译错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20261037/

10-12 20:27