i am working on the output from the stack and list but i got 2 errors and i dont know how to fix it, hope some1 can help me to figure it out
thank you

header.h

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Lexeme
{
private:
	string Lex;
	string type;

public:


	Lexeme();
	string getLex ();
	string gettype ();
	void setLex (string);
	void settype (string);
};
#include "TheHeader.h"
#include <stack>
#include <list>


void ReadFile(); 

template <typename T>
	T StringToNumber ( const string &Text)
	{
		stringstream ss(Text);
		T result;
		return ss >> result ? result : 0;
	}

int main ()
{
	ReadFile();
	return 0;
}
void ReadFile()
{
	char a;
	string str = "";
	Lexeme temp;
	stack <Lexeme> name;
	list <Lexeme> listD;
	int size;

	ifstream file ("sample.txt",ios::in);
	if(!file)
	{
		cout<<"Unable to open the file!!!" <<endl;
		exit(1);
	}
	file >> a;

	while (file)
	{
		if (a == '/' ||a == '*' ||a == '+' ||a == '-' ||a == ';' ||a == '^'||a == '='||a == '('||a == ')')
		{
		cout << a << " symbol" << endl ;
		str = a;
		temp.setLex(str);
		temp.settype("SYMBOL");
		listD.push_back(temp);
		name.push(temp);
		file >> a;
		}

	else {	str = a;file >> a;
			while( a >= '0' && a <= '9' || a == '.')
				{
				str.append(1,a);
				file >> a;
				}
			temp.setLex(str);
			temp.settype("NUMBER");
			listD.push_back(temp);
			name.push(temp);
			cout << str << " number" <<endl ;
			str = "";
	}
	
	}
	cout << " from the list: " << endl;
	list <Lexeme>::iterator place;
	for(place = listD.begin(); place != listD.end(); ++place)
	{
		cout<<*place<<" "<<endl;
		size = name.size();
	}
	cout << " stack: "<< endl;
	for (int i=0; i < size; i++)
	{
		cout<<name.top()<<" ";
		name.pop();
	}


	file.close();
}

header.cpp

#include "TheHeader.h"



Lexeme::Lexeme()
{
Lex = "";
type ="";
}
	string Lexeme::getLex ()
	{
		return Lex;
	}
	string Lexeme::gettype ()
	{
		return type;
	}
	void Lexeme::setLex(string b)
	{
		Lex = b;
	}
	void Lexeme::settype (string c)
	{
		type = c;
	}

and the error i got is:

Error	1	error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Lexeme' (or there is no acceptable conversion)		

Error	2	error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Lexeme' (or there is no acceptable conversion)

Recommended Answers

All 3 Replies

the problem is on line 70 and 76. on line 76 you have cout<<name.top()<<" "; . here you are outputting the top object int the stack which is of type Lexeme. you either need to define and operator<<() function for your class or call an accessor like cout<<name.top().getLex()<<" "; . the same goes for line 70 when you are calling the list object which is of type Lexeme. hope this makes sense to you.

the problem is on line 70 and 76. on line 76 you have cout<<name.top()<<" "; . here you are outputting the top object int the stack which is of type Lexeme. you either need to define and operator<<() function for your class or call an accessor like cout<<name.top().getLex()<<" "; . the same goes for line 70 when you are calling the list object which is of type Lexeme. hope this makes sense to you.

ty for ur suggestion, it worked for line 76, bt i dont know how to fix line 70, could u give me hint? ty

i would do something along the lines of cout << *place.GetLex() << endl;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.