7 Replies - 369 Views - Last Post: 09 October 2012 - 03:06 PM Rate Topic: -----

#1 telecom2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-January 12

I have this program with two Errors

Posted 09 October 2012 - 12:09 PM

there are two errors in red color highlight it


#ifndef List02
#define List02 1
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
struct ListException
{
	ListException(char* m)
	{
		cout<<endl;
		cout<<"I am the list and I am"<<m<<".";
		cout<<endl;
	}
};
class List
{
	int size;
	int count;
	string* L;
	int cursor;
public:
	List(int sz=100):size(sz),count(0),cursor(-1){L=new string[size];}
	List(istream& i,int sz=100):size(sz),count(0),cursor(-1){L=new string[size];Scan(i);}
	~List(){if(L) delete[]L;}
	bool IsFull(void)
	{
		return count>=size-1;
	}
	void Insert(string v)
	{
		if(IsFull()) throw ListException("full");
		L[count++]=v;
	}
	void Scan(istream& i)
	{
		for(;;)/>
		{
			string v;
			i>>v; if(i.eof())break;
			Insert(v);
		}
	}
		void Print(ostream& o, const char* title)
	{
		o<<endl;
		o<<title<<"={";
		for(int a=0;a<count;a++)
		{
			if(a!=0) o<<",";
			o<<L[a];
		}
		o<<"}";
		o<<endl;
	}
	void First(void){cursor=0;}
void Next(void){if (cursor<count) cursor++;}
bool IsEol(void)[color="#FF0000"]
{ if (cursor<0||cursor>count) throw ListException(cursor,count); //C2661:'ListException::ListException' : no overloaded[/color] function takes 2 
return cursor>=count;
}
string Member(void)
[color="#FF0000"]{ if (cursor<0||cursor>=count) throw ListException(cursor,count); //C2661:'ListException::ListException' : no overloaded[/color]
return L[cursor];
}
};
#endif

This post has been edited by Salem_c: 09 October 2012 - 12:25 PM
Reason for edit:: added [code][/code] tags - learn to use them yourself


Is This A Good Question/Topic? 0
  • +

Replies To: I have this program with two Errors

#2 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: I have this program with two Errors

Posted 09 October 2012 - 12:30 PM

> 62 { if (cursor<0||cursor>count) throw ListException(cursor,count);
Perhaps somewhere between lines 10 and 18, you might add another ListException taking 2 parameters?

> 23 string* L;
Have you considered std::vector<string> L; as a safer alternative?
Was This Post Helpful? 0
  • +
  • -

#3 telecom2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-January 12

Re: I have this program with two Errors

Posted 09 October 2012 - 12:38 PM

No it is string not vector
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,568
  • Joined: 25-December 09

Re: I have this program with two Errors

Posted 09 October 2012 - 12:56 PM

Well since it is a std::string you don't need to use the pointer or new. A std::string will will grow and shrink automatically so you don't need to worry about the memory.

class List
{
	int size;
	int count;
	//string* L;
        string L;
	int cursor;
public:
	List(int sz=100):size(sz),count(0), L(), cursor(-1){}

And since you are using the size for your new[] you probably won't need this variable either, since a std::string knows it's size.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: I have this program with two Errors

Posted 09 October 2012 - 01:01 PM

> L=new string[size];
...
> 36 L[count++]=v;
What is this then, if not an array of strings?
Was This Post Helpful? 0
  • +
  • -

#6 telecom2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-January 12

Re: I have this program with two Errors

Posted 09 October 2012 - 02:42 PM

Still the same problem in line 66
// error C2661: 'ListException::ListException' : no overloaded function takes 2 arguments
bool IsEol(void)
{ if (cursor<0||cursor>count) throw ListException(cursor,count);
return cursor>=count;
}
string Member(void)
{ if (cursor<0||cursor>=count) throw ListException(cursor,count);
return L[cursor];
}
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,568
  • Joined: 25-December 09

Re: I have this program with two Errors

Posted 09 October 2012 - 02:45 PM

Post your modified code.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 telecom2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-January 12

Re: I have this program with two Errors

Posted 09 October 2012 - 03:06 PM

#ifndef List02
#define List02 1
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
struct ListException
{
	ListException(char* m)
	{
		cout<<endl;
		cout<<"I am the list and I am"<<m<<".";
		cout<<endl;
	}
};
class List
{
	int size;
	int count;
	[color="#FF0000"]string L;[/color]
	int cursor;
public:
	[color="#FF0000"]List(int sz=100):size(sz),count(0), L(), cursor(-1){[/color]L=new string[size];}
	List(istream& i,int sz=100):size(sz),count(0),cursor(-1){L=new string[size];Scan(i);}
	~List(){if(L) delete[]L;}
	bool IsFull(void)
	{
		return count>=size-1;
	}
	void Insert(string v)
	{
		if(IsFull()) throw ListException("full");
		L[count++]=v;
	}
	void Scan(istream& i)
	{
		for(;;)/>
		{
			string v;
			i>>v; if(i.eof())break;
			Insert(v);
		}
	}
		void Print(ostream& o, const char* title)
	{
		o<<endl;
		o<<title<<"={";
		for(int a=0;a<count;a++)
		{
			if(a!=0) o<<",";
			o<<L[a];
		}
		o<<"}";
		o<<endl;
	}
	void First(void){cursor=0;}
void Next(void){if (cursor<count) cursor++;}
bool IsEol(void)
{ if (cursor<0||cursor>count) throw ListException(cursor,count); 
return cursor>=count;
}
string Member(void)
{ if (cursor<0||cursor>=count) throw ListException(cursor,count); 
return L[cursor];
}
};
#endif

This post has been edited by jimblumberg: 09 October 2012 - 03:29 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1