Witam,
chciałem sobie napisać pewien program. Dodałem sobie dwa pliki (*.cpp i *.h). W pliku nagłówkowym jest deklaracja klasy, w namespace. Kiedy próbuję wywołać konstruktor dostaję błąd:
D:\AppData\Tmp\cczno6HG.o:main.cpp:(.text+0x171): undefined reference to `ArgP::
ArgumentParser::ArgumentParser(int, char**)'
collect2.exe: error: ld returned 1 exit status
(tak swoją drogą nie wiem dlaczego ta ścieżka to D:\AppData\...)
Tutaj kod:
main.cpp
#include <iostream>
#include <fstream>
#include <windows.h>
#include "ArgParser.h"
void color(int color){
static HANDLE ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(ConsoleHandle, color);
}
bool IsExist(const char* Path){
static std::ifstream stream(Path);
if(!stream.is_open()) return false;
return true;
}
int main(int argc, char* argv[]){
ArgP::ArgumentParser ArgParser(argc, argv);
}
ArgParser.h:
#include <string>
#include <vector>
#include <string.h>
namespace ArgP{
std::string EMPTY_ERR = "0-EmptySentenceError-0";
std::string OFB_ERR = "0-OutOfBoundError-0";
class ArgumentParser{
public:
ArgumentParser(int ArgCount, char* ArgVal[]);
~ArgumentParser() = default;
std::string operator[](int index);
private:
int ArgCount = 0;
int max = 0;
std::vector<std::string> ArgValues;
};
}
ArgParser.cpp:
#include "ArgParser.h"
ArgP::ArgumentParser::ArgumentParser(int ArgCount, char* ArgVal[]){
this->max = ArgCount;
ArgValues.reserve(ArgCount);
for(int i=0; i<ArgCount; ++i){
if(strlen(const_cast<const char*>(ArgVal[i]))==0){
ArgValues[i] = EMPTY_ERR;
continue;
}
ArgValues[i] = std::string(ArgVal[i]);
}
}
std::string ArgP::ArgumentParser::operator[](int index){
if(index>=max){
return OFB_ERR;
}
return this->ArgValues[index];
}
Nie wiem gdzie popełniłem błąd.
Z góry dziękuję każdej osobie która poświęciła swój czas.
Pozdrawiam.
PS program kompiluję z konsoli:
g++ -std=c++17 main.cpp
MinGW:
g++ (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.