hi,
volna egy library, s egy class aminek adatokat kéne tárolnia egy linked list of structures szerűségben. mivel, még úgyis szükség lessz rá, készítettem egy class-t, hogy az kezelje a listát.. template, mert több féle elemet akarok tárolni.. aztán itt vany a gond, linkelésnél egy halom undefined reference hibaüzenet.. keresgélés után megtaláltam, hogy az export lenne a legjobb, de azt meg szinte egy fordító sem támogatja :(... a typedef-es és a c++ fájl bele a headerbe is próbáltam de egyik se akar működni...
/**
* @file core/linkedlist.h++
* A class for linked list of structures
*/
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
namespace DW {
template<class type>
class LinkedList {
private:
struct ListStruct {
type item;
ListStruct * next;
} *start, *last;
unsigned int length;
public:
LinkedList();
~LinkedList();
type * Add();
type * Add(type data);
type & operator [] (unsigned int index);
unsigned int GetLength() { return length; }
void Remove(unsigned int id);
};
typedef LinkedList<struct DirtYOptions> OptionsLinkedList;
}
#endif
/**
* @file core/linkedlist.c++
* A class for linked list of structures
*/
#include "linkedlist.h++"
#include <stdexcept>
using namespace DW;
template<class type>
LinkedList<type>::LinkedList()
{
length = 0;
*start = NULL;
*last = NULL;
}
template<class type>
LinkedList<type>::~LinkedList()
{
ListStruct * current, next;
current = start;
while (current != NULL) {
next = current->next;
delete current;
current = next;
}
}
template<class type>
type * LinkedList<type>::Add()
{
last = last->next = new ListStruct;
length++;
return &last->item;
}
template<class type>
type * LinkedList<type>::Add(type data)
{
last = last->next = new type;
last->item = data;
length++;
return &last->item;
}
template<class type>
type & LinkedList<type>::operator[](unsigned int index)
{
if (index < 0 || index > length) throw std::out_of_range::out_of_range("DW::LinkedList::operator []");
ListStruct * current;
current = start;
for (unsigned int i = 0; i < index; i++)
current = current->next;
return current->item;
}
/**
* @file core/parameters.h++
* Header for parameter-handling routines
*/
#ifndef PARAMETERS_H
#define PARAMETERS_H
#include <getopt.h>
#include <iostream>
#include "linkedlist.h++"
//template<class type> struct LinkedList {unsigned int num; };
namespace DW {
struct DirtYOptions {
char short_option; ///< Short parameter <em>(eg: -c)</em>
std::string long_option; ///< Long parameter <em>(eg: --something)</em>
std::string help_text; ///< Help text displayed for the parameter
int *variable_change; ///< Variable to change, when the option specified
int variable_set_to; ///< The value, that variable_change set to
bool has_argument; ///< <b>True</b>, if the parameter has an argument
std::string *argument_change; ///< The variable, that gets the argument
}; ///< Stores information for processing program parameters
class Params
{
public:
Params();
// ~Params();
/**
* @brief Adds a parameter to the list.
* @param short_option the short option for that parameter <em>(eg: -c)</em>. Set it to 0, if you do not want to have short option.
* @param long_option the long option for that parameter <em>(eg: --something)</em>
* @param help_text the message that displayed to the user, when running with <em>--help</em>
* @param var_change a pointer to an int variable, that's get changed when the user specifies the option
* @param var_set_to sets var_change to the specified value when the option specified
* @param has_argument if true, DW will look for a argument for that option <em>(eg: --something boo)</em>
* @param argument_change a pointer to an std::string variable that gets the argument value. <b>Note:</b> The program gets every argument as a string. If you need a numerical value, hou have to convert it manually.
*/
void AddParameter(char short_option, std::string long_option, std::string help_text, int *var_change, int var_set_to, bool has_argument=false, std::string *argument_change=NULL);
void SetInfo(std::string info); ///< Sets @link DW_Params::info_text info_text @endlink
void SetHelp(std::string help); ///< Sets @link DW_Params::help_text help_text @endlink
/**
* @brief Run the check on the program's parameters.
* @note SetHelp(), SetInfo() and all AddParameter() functions have to ran before you execute this command.
* @param argc and @param argv are the parameters, that the main function gets (eg: <pre style="display: inline;">int main(int argc, char* argv[]) { ...</pre>)
*/
void Run(int argc, char* argv[]);
private:
OptionsLinkedList args; ///< Contains the parameters
std::string help_text; ///< A string, that displayed when the user runs the program with <em>--help</em>
std::string info_text; ///< A string, that displayed when the user runs the program with <em>--usage</em> (usually contains the program name and version)
};
}
#endif
/**
* @file core/parameters.c++
* Handling applications parameters
* Routines to handle the application's parameters
*/
#include <cstdlib>
#include <cassert>
#include "parameters.h++"
using namespace DW;
Params::Params()
{
//args.num = 0;
help_text = "Undefined";
info_text = "Undefined";
}
void Params::SetInfo(std::string info)
{
info_text = info;
}
void Params::SetHelp(std::string help)
{
help_text = help;
}
void Params::AddParameter(char short_option, std::string long_option, std::string help_text, int *var_change, int var_set_to, bool has_argument, std::string *argument_change)
{
assert(long_option!="");
assert(var_change);
if (has_argument) assert(argument_change);
DirtYOptions * opts = args.Add();
opts->short_option = short_option;
opts->long_option = long_option;
opts->help_text = help_text;
opts->variable_change = var_change;
opts->variable_set_to = var_set_to;
opts->has_argument = has_argument;
opts->argument_change = argument_change;
}
void Params::Run(int argc, char* argv[])
{
std::cout<<"Info text: "<<info_text<<std::endl;
std::cout<<"Help text: "<<help_text<<std::endl;
for (unsigned int i=0; i<args.GetLength(); i++)
std::cout<<i<<"th parameter: "<<args[i].long_option<<std::endl;
std::cout<<std::endl;
for (int i=0; i<argc; i++)
std::cout<<i<<"th argument: "<<argv[i]<<std::endl;
exit(0);
}
ez a library, ehez egy példa program:
#include <parameters.h++>
int main(int argc, char* argv[])
{
DW::Params Params;
int f = 0;
Params.SetHelp("This is a test program. And this is it's help text. Read carefully!");
Params.SetInfo("TesT program v. 0.0");
Params.AddParameter(0, "foobar", "Something foo+bar=foobar!!! :@", &f, 1);
Params.Run(argc,argv);
return 0;
}
és a hibaüzenetek:
/tmp/cc5Fwx7x.o: In function `DW::Params::~Params()':
test.cpp:(.text._ZN2DW6ParamsD1Ev[DW::Params::~Params()]+0x57): undefined reference to `DW::LinkedList<DW::DirtYOptions>::~LinkedList()'
test.cpp:(.text._ZN2DW6ParamsD1Ev[DW::Params::~Params()]+0x70): undefined reference to `DW::LinkedList<DW::DirtYOptions>::~LinkedList()'
build/default/core//libdw_core.a(parameters.o): In function `DW::Params::Run(int, char**)':
../src/core/parameters.c++:87: undefined reference to `DW::LinkedList<DW::DirtYOptions>::operator[](unsigned int)'
build/default/core//libdw_core.a(parameters.o): In function `DW::Params::AddParameter(char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int*, int, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)':
../src/core/parameters.c++:61: undefined reference to `DW::LinkedList<DW::DirtYOptions>::Add()'
build/default/core//libdw_core.a(parameters.o): In function `Params':
../src/core/parameters.c++:36: undefined reference to `DW::LinkedList<DW::DirtYOptions>::LinkedList()'
../src/core/parameters.c++:41: undefined reference to `DW::LinkedList<DW::DirtYOptions>::~LinkedList()'
../src/core/parameters.c++:36: undefined reference to `DW::LinkedList<DW::DirtYOptions>::LinkedList()'
../src/core/parameters.c++:41: undefined reference to `DW::LinkedList<DW::DirtYOptions>::~LinkedList()'
előre is köszi minden segítséget... ja, és most kb 1 óráig nem leszek internet közelben...