Hi! I'm getting this error when compiling:
$ g++ index.cpp
index.cpp:85: error: 'friend' used outside of class
index.cpp: In function 'std::ostream& operator<<(std::ostream&, const GrabStuff&)':
index.cpp:88: error: 'myLineNumbers' was not declared in this scope


Links to included files:
mystring.h
string.cpp
SplayTree.h
QueueAr.h

And for fun:
SplayTree.cpp
QueueAr.cpp

#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
#include "mystring.h"
#include "string.cpp"
#include "SplayTree.h"
#include "QueueAr.h"

class GrabStuff
{
	public:
		GrabStuff();
		GrabStuff(string);
		~GrabStuff();
		void setWord (string);
		string getWord ();
		void setLineNumber (int);
		[B]friend ostream& operator<< (ostream &, const GrabStuff&);[/B]
	private:	
		string myWord;
		Queue<int> myLineNumbers;
		Queue<int> temp;
};

GrabStuff::GrabStuff()
{}

GrabStuff::GrabStuff(string s) {     myWord = s;     }

GrabStuff::~GrabStuff() {}

void GrabStuff::setWord (string w) {	    myWord = w;     }

string GrabStuff::getWord () {     return myWord;     }

void GrabStuff::setLineNumber (int num) {     myLineNumbers.enqueue(num);     }

ostream& operator<< (ostream &os, const GrabStuff &name)
{
	os << name.myWord << " ";
	[B]while (!myLineNumbers.isEmpty())[/B]
	{
		// blahblah		
	}
	return os;
}

What am I doing wrong? ='( I've literally looked at this for three hours. HELP! =) Thanks.

Sucesso commented: Went extra mile to post. :) +0

Recommended Answers

All 3 Replies

Hi! I'm getting this error when compiling:
$ g++ index.cpp
index.cpp:85: error: 'friend' used outside of class
index.cpp: In function 'std::ostream& operator<<(std::ostream&, const GrabStuff&)':
index.cpp:88: error: 'myLineNumbers' was not declared in this scope


Links to included files:
mystring.h
string.cpp
SplayTree.h
QueueAr.h

And for fun:
SplayTree.cpp
QueueAr.cpp

#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
#include "mystring.h"
#include "string.cpp"
#include "SplayTree.h"
#include "QueueAr.h"

class GrabStuff
{
	public:
		GrabStuff();
		GrabStuff(string);
		~GrabStuff();
		void setWord (string);
		string getWord ();
		void setLineNumber (int);
		[B]friend ostream& operator<< (ostream &, const GrabStuff&);[/B]
	private:	
		string myWord;
		Queue<int> myLineNumbers;
		Queue<int> temp;
};

GrabStuff::GrabStuff()
{}

GrabStuff::GrabStuff(string s) {     myWord = s;     }

GrabStuff::~GrabStuff() {}

void GrabStuff::setWord (string w) {	    myWord = w;     }

string GrabStuff::getWord () {     return myWord;     }

void GrabStuff::setLineNumber (int num) {     myLineNumbers.enqueue(num);     }

ostream& operator<< (ostream &os, const GrabStuff &name)
{
	os << name.myWord << " ";
	[B]while (!myLineNumbers.isEmpty())[/B]
	{
		// blahblah		
	}
	return os;
}

What am I doing wrong? ='( I've literally looked at this for three hours. HELP! =) Thanks.

i am not sure but it might be
friend ostream& operator<< (ostream &, const GrabStuff&);
needs a space between operator and <<
so it would be
friend ostream& operator << (ostream &, const GrabStuff&);

i am not sure but it might be
friend ostream& operator<< (ostream &, const GrabStuff&);
needs a space between operator and <<
so it would be
friend ostream& operator << (ostream &, const GrabStuff&);

That is not an issue. I dont know why that error is coming though but I have tested it without space and works fine.


Error 2>

ostream& operator<< (ostream &os, const GrabStuff &name)
{
	os << name.myWord << " ";
	while (!myLineNumbers.isEmpty()) /*IN THIS LINE*******/
	{
		// blahblah		
	}
	return os;
}

Should have done

while (!name.myLineNumbers.isEmpty())
commented: fixed my problem +0

dkalita, you are a genius. Thank you SO MUCH! <3

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.