gpt4 book ai didi

C++ LogFile 对象编译错误

转载 作者:行者123 更新时间:2023-11-28 03:03:14 24 4
gpt4 key购买 nike

<分区>

我正在底部创建一个日志文件对象是.h 和.cc

下面是我得到的错误,请帮助

Undefined symbols for architecture x86_64:
"LogFile::log(std::__1::basic_string, std::__1::allocator >)", referenced from: TextDisplay::notify(int, int, Cell::CellType) in textdisplay.o Floor::loadFloor(std::__1::basic_string, std::__1::allocator >, Player*) in floor.o Floor::isStairwaySet() in floor.o Floor::getCellAtCoord(int, int) in floor.o Floor::getPossibleSpots(Occupant*, int, int) in floor.o Floor::getNearbyPlayer(int, int) in floor.o Character::dealDamage(int) in character.o ... "LogFile::log(char const*)", referenced from: CC3K::cleanUp() in cc3k.o CC3K::startGame(Occupant::SpecificType, std::__1::basic_string, std::__1::allocator >) in cc3k.o CC3K::step() in cc3k.o CC3K::playerMove(CC3K::Command, CC3K::Direction) in cc3k.o Floor::loadFloor(std::__1::basic_string, std::__1::allocator >, Player*) in floor.o Floor::notifyStairwayBeingSet(int, int) in floor.o Cell::removeDeadOccupant() in cell.o ... "LogFile::dlog(std::__1::basic_string, std::__1::allocator >)", referenced from: Merchant::attackStep() in merchant.o "LogFile::dlog(char const*)", referenced from: Character::attack(Character&) in character.o Merchant::attackStep() in merchant.o "LogFile::getI()", referenced from: CC3K::cleanUp() in cc3k.o CC3K::startGame(Occupant::SpecificType, std::__1::basic_string, std::__1::allocator >) in cc3k.o CC3K::step() in cc3k.o CC3K::playerMove(CC3K::Command, CC3K::Direction) in cc3k.o TextDisplay::notify(int, int, Cell::CellType) in textdisplay.o Floor::loadFloor(std::__1::basic_string, std::__1::allocator >, Player*) in floor.o Floor::notifyStairwayBeingSet(int, int) in floor.o ... ld: symbol(s) not found for architecture 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;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com