带有模板的字符串不起作用? [英] Strings with Templates not working?

查看:56
本文介绍了带有模板的字符串不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在使用模板来模拟来自Java的LinkedList。它可以在没有

问题的情况下工作,但是当我想要使用时main.cpp中的字符串而不是char *,我

得到以下错误信息:


$ ./Main

终止抛出''std :: logic_error''实例后调用

what():basic_string :: _ S_construct NULL无效


我试图导入< ; mainbpp和listT.h中的stringboth,但

errormsg是一样的。


这些类看起来像这样:


MAIN.CPP ----------------------------------------- -

#include" ListT.h"

#include< iostream>


using namespace std;


int main(无效){


ListT< string * testList = new ListT< string>();

testList - > add(" Test1");

testList-> add(" Test2");

testList-> add(" Te st3");

cout<< testList-> get(0)<< endl;

cout<< testList-> get(1)<< endl;

cout<< testList-> get(2)<< endl;

删除testList;


返回0;

}

------------------------------------------- -----


LISTT.H ------------------------------ -----------


模板< class Tclass ListT {


private:

struct TKnoten {

T daten;

int index;

TKnoten * next;

};

int counter;

TKnoten * kopf;


public:

ListT();

virtual~ListT();

void add(T element);

// void remove(T element);

T get(int i);

};


模板< class T>

ListT< T>: :ListT(){

counter = 0;

kopf = new TKnoten();

kopf-> next = 0;

}


模板< class T>

ListT< T> ::〜ListT(){

while(kopf-> next!= 0){

TKnoten * previous = new TKn oten();

previous = kopf;

kopf = kopf-> next;

删除之前;

}

删除kopf;

}


模板< class T>

void ListT< ; T> :: add(T元素){

TKnoten * newKnoten = new TKnoten();


newKnoten-> next = kopf;

kopf = newKnoten;

kopf-> daten = element;

kopf-> index = counter;

counter ++;

}


template< class T>

T ListT< T> :: get(int i){

TKnoten * iterator = new TKnoten();

T daten = 0;

iterator = kopf;

while(iterator-> next!= 0){

if(iterator-> index == i){

daten = iterator-> daten;

}

iterator = iterator-> next;

}

删除迭代器;

return daten;

}


字符串类型有什么问题?我正在使用Linux和gcc(g ++),

感谢您的回答,


Markus

Hello,

I''m using a template to simulate a LinkedList from Java.It works without
problems, but when I want to use strings in main.cpp instead of char*, I
get the following error message:

$ ./Main
terminate called after throwing an instance of ''std::logic_error''
what(): basic_string::_S_construct NULL not valid

I tried to import <stringboth in main.cpp and listT.h, but the
errormsg was the same.

The classes look like this:

MAIN.CPP------------------------------------------
#include "ListT.h"
#include <iostream>

using namespace std;

int main(void) {

ListT<string*testList = new ListT<string>();
testList->add("Test1");
testList->add("Test2");
testList->add("Test3");
cout << testList->get(0) <<endl;
cout << testList->get(1) <<endl;
cout << testList->get(2) <<endl;
delete testList;

return 0;
}
------------------------------------------------

LISTT.H-----------------------------------------

template <class Tclass ListT {

private:
struct TKnoten {
T daten;
int index;
TKnoten *next;
};
int counter;
TKnoten *kopf;

public:
ListT();
virtual ~ListT();
void add(T element);
//void remove(T element);
T get(int i);
};

template <class T>
ListT<T>::ListT() {
counter = 0;
kopf = new TKnoten();
kopf->next = 0;
}

template <class T>
ListT<T>::~ListT() {
while (kopf->next != 0) {
TKnoten *previous = new TKnoten();
previous = kopf;
kopf = kopf->next;
delete previous;
}
delete kopf;
}

template <class T>
void ListT<T>::add(T element) {
TKnoten *newKnoten = new TKnoten();

newKnoten->next = kopf;
kopf = newKnoten;
kopf->daten = element;
kopf->index = counter;
counter++;
}

template <class T>
T ListT<T>::get(int i) {
TKnoten *iterator = new TKnoten();
T daten = 0;
iterator = kopf;
while (iterator->next != 0) {
if (iterator->index == i) {
daten = iterator->daten;
}
iterator = iterator->next;
}
delete iterator;
return daten;
}

What''s wrong with string as type? I''m using Linux with gcc (g++),
Thanks for your answers,

Markus

推荐答案

./ Main

在抛出''std :: logic_error'的实例后终止调用

what():basic_string :: _S_construct NULL无效


我试图在main.cpp和listT.h中导入< stringboth,但

errormsg是相同的。


课程如下:


MAIN.CPP -------------------- ----------------------

#include" ListT.h"

#include< iostream>


使用命名空间std;


int main(无效){


ListT< string * testList = new ListT< string>();

testList-> add(" Test1");

testList-> add(" Test2" );

testList-> add(" Test3");

cout<< testList-> get(0)<< endl;

cout<< testList-> get(1)<< endl;

cout<< testList-> get(2)<< endl;

删除testList;


返回0;

}

------------------------------------------- -----


LISTT.H ------------------------------ -----------


模板< class Tclass ListT {


private:

struct TKnoten {

T daten;

int index;

TKnoten * next;

};

int counter;

TKnoten * kopf;


public:

ListT();

virtual~ListT();

void add(T element);

// void remove(T element);

T get(int i);

};


模板< class T>

ListT< T>: :ListT(){

counter = 0;

kopf = new TKnoten();

kopf-> next = 0;

}


模板< class T>

ListT< T> ::〜ListT(){

while(kopf-> next!= 0){

TKnoten * previous = new TKn oten();

previous = kopf;

kopf = kopf-> next;

删除之前;

}

删除kopf;

}


模板< class T>

void ListT< ; T> :: add(T元素){

TKnoten * newKnoten = new TKnoten();


newKnoten-> next = kopf;

kopf = newKnoten;

kopf-> daten = element;

kopf-> index = counter;

counter ++;

}


template< class T>

T ListT< T> :: get(int i){

TKnoten * iterator = new TKnoten();

T daten = 0;

iterator = kopf;

while(iterator-> next!= 0){

if(iterator-> index == i){

daten = iterator-> daten;

}

iterator = iterator-> next;

}

删除迭代器;

return daten;

}


字符串类型有什么问题?我正在使用Linux和gcc(g ++),

感谢您的回答,


Markus
./Main
terminate called after throwing an instance of ''std::logic_error''
what(): basic_string::_S_construct NULL not valid

I tried to import <stringboth in main.cpp and listT.h, but the
errormsg was the same.

The classes look like this:

MAIN.CPP------------------------------------------
#include "ListT.h"
#include <iostream>

using namespace std;

int main(void) {

ListT<string*testList = new ListT<string>();
testList->add("Test1");
testList->add("Test2");
testList->add("Test3");
cout << testList->get(0) <<endl;
cout << testList->get(1) <<endl;
cout << testList->get(2) <<endl;
delete testList;

return 0;
}
------------------------------------------------

LISTT.H-----------------------------------------

template <class Tclass ListT {

private:
struct TKnoten {
T daten;
int index;
TKnoten *next;
};
int counter;
TKnoten *kopf;

public:
ListT();
virtual ~ListT();
void add(T element);
//void remove(T element);
T get(int i);
};

template <class T>
ListT<T>::ListT() {
counter = 0;
kopf = new TKnoten();
kopf->next = 0;
}

template <class T>
ListT<T>::~ListT() {
while (kopf->next != 0) {
TKnoten *previous = new TKnoten();
previous = kopf;
kopf = kopf->next;
delete previous;
}
delete kopf;
}

template <class T>
void ListT<T>::add(T element) {
TKnoten *newKnoten = new TKnoten();

newKnoten->next = kopf;
kopf = newKnoten;
kopf->daten = element;
kopf->index = counter;
counter++;
}

template <class T>
T ListT<T>::get(int i) {
TKnoten *iterator = new TKnoten();
T daten = 0;
iterator = kopf;
while (iterator->next != 0) {
if (iterator->index == i) {
daten = iterator->daten;
}
iterator = iterator->next;
}
delete iterator;
return daten;
}

What''s wrong with string as type? I''m using Linux with gcc (g++),
Thanks for your answers,

Markus


2007年6月20日星期三21:41:13 +0200,Markus Pitha写道:
On Wed, 20 Jun 2007 21:41:13 +0200, Markus Pitha wrote:

您好,


我正在使用模板来模拟来自Java的LinkedList。它没有

问题,但是当我想在main.cpp中使用字符串而不是char *时,我

收到以下错误消息:

Hello,

I''m using a template to simulate a LinkedList from Java.It works without
problems, but when I want to use strings in main.cpp instead of char*, I
get the following error message:


./ Main

在抛出''std实例后终止调用: :logic_error''

what():basic_string :: _ S_construct NULL无效


我试图在main.cpp和listT.h中导入< stringboth ,但

errormsg是一样的。


课程如下:
./Main
terminate called after throwing an instance of ''std::logic_error''
what(): basic_string::_S_construct NULL not valid

I tried to import <stringboth in main.cpp and listT.h, but the
errormsg was the same.

The classes look like this:



[snip]

[snip]


template< class T>

T ListT< T> :: get(int i){

TKnoten * iterator = new TKnoten();

T daten = 0;
template <class T>
T ListT<T>::get(int i) {
TKnoten *iterator = new TKnoten();
T daten = 0;



!---->> ^^^^^^^^^^^


T daten ="" ;;

!---->>^^^^^^^^^^^

T daten = "";


iterator = kopf;

while(iterator-> next!= 0){

if(iterator-> index == i){

daten = iterator-> daten;

}

iterator = iterator-> next;

}

删除迭代器;

返回daten;

}


字符串类型有什么问题?我正在使用Linux和gcc(g ++),
iterator = kopf;
while (iterator->next != 0) {
if (iterator->index == i) {
daten = iterator->daten;
}
iterator = iterator->next;
}
delete iterator;
return daten;
}

What''s wrong with string as type? I''m using Linux with gcc (g++),



-

令人讨厌的用户

--
Obnoxious User


这篇关于带有模板的字符串不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆