Jump to content

I am in need of some assistance. I am doing my first course on C++ and the course assumes that we did a previous course, which my uni degree does not include. So lectures kinda skip a lot of stuff. Anyway I am trying to overload the << operator for cout. This is to print a queue class which we needed to create. I think I have it all done but I am getting this error message from cygwin. 

 

g++  main.o node.o linkedlist.o queue.o -o ConnectFour
queue.o:queue.cpp:(.text+0x151): undefined reference to `banks_assignment1::operator<<(std::ostream&, banks_assignment1::node&)'
queue.o:queue.cpp:(.text+0x151): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `banks_assignment1::operator<<(std::ostream&, banks_assignment1::node&)'
collect2: error: ld returned 1 exit status
makefile:11: recipe for target 'ConnectFour' failed
make: *** [ConnectFour] Error 1

My PC:

Case: NZXT Phantom 420 | Motherboard: Gigabyte GA-Z77X-UD3H-WB WIFI | PSU: Corsair TX650M | CPU: Intel 3570k | CPU Cooler: Corsair H60 with Corsair SP120s in push pull | GPU: AMD 7970 GV-R797OC-3GD | RAM: 16GB Corsair Vengeance 4x4GB | SSD: Samsung 830 256GB | HDD: Seagate Barracuda 2TB
Link to comment
https://linustechtips.com/topic/219959-c-assistance/
Share on other sites

Link to post
Share on other sites

Gunna need to see the code to help.

Sorry. 

std::ostream& operator << (std::ostream& out, const queue& queue){  node* current = queue.data->get_head();   while (current != NULL)  {   out << current->get_data() << " "; //Prints current position then leaves a gap.   current = current->get_next();  }  //Ending the row with a #.  out << "#";  return out;}

My PC:

Case: NZXT Phantom 420 | Motherboard: Gigabyte GA-Z77X-UD3H-WB WIFI | PSU: Corsair TX650M | CPU: Intel 3570k | CPU Cooler: Corsair H60 with Corsair SP120s in push pull | GPU: AMD 7970 GV-R797OC-3GD | RAM: 16GB Corsair Vengeance 4x4GB | SSD: Samsung 830 256GB | HDD: Seagate Barracuda 2TB
Link to comment
https://linustechtips.com/topic/219959-c-assistance/#findComment-3015056
Share on other sites

Link to post
Share on other sites

You need to make sure the ostream overloaded operator is declared in the global scope:

 

Correct:

//queue.h#include <string>#include <iostream>...class queue{public:...private:....};std::ostream& operator<<(std::ostream& os, const queue& instance) {  ...}

Incorrect:

//queue.h#include <string>#include <iostream>...class queue{public:...std::ostream& operator<<(std::ostream& os);...}private:....};

You need to define a function within your class (Queue) that returns a std::ostream& and do something like this:

//queue.h#include <string>#include <iostream>...class queue{public:...std::ostream& addToStream(std::ostream& os);private:...};std::ostream& operator<<(std::ostream& os, const queue& instance) {   return instance.addToStream(os);}

Function definition for addToStream:

std::ostream& queue::addToStream(std::ostream& os){  node* current = data->get_head();   while (current != NULL)  {   os << current->get_data() << " "; //Prints current position then leaves a gap.   current = current->get_next();  }  //Ending the row with a #.  os << "#";  return os;}

Hope that helps!

| CPU: 2600K @ 4.5 GHz 1.325V | MB: ASUS P8Z68-V pro | GPU: EVGA GTX 480 clk 865/mem 2100 | RAM: Corsair Vengeance 1600 MHz CL9 | HDD: Muskin Chronos Deluxe 240GB(win8) && ADATA SX900 120 GB(ubuntu 12.04) | PSU: Seasonic x760 |

Link to comment
https://linustechtips.com/topic/219959-c-assistance/#findComment-3037622
Share on other sites

Link to post
Share on other sites

It can be declared inside of a class fine.

You can, but it makes using the syntax look strange:

myQueue << std::cout

I believe this is vastly more readable, and consistent with the standard notation:

std::cout << myQueue << std::endl;std::cout << "End of queue output" << std::endl;

Further, if you want avoid polluting the global namespace, there are ways of adding indirection, but it is not necessary for his/her task I'm sure.

 

Edit: Sorry I made I typo in my original post. Corrected

| CPU: 2600K @ 4.5 GHz 1.325V | MB: ASUS P8Z68-V pro | GPU: EVGA GTX 480 clk 865/mem 2100 | RAM: Corsair Vengeance 1600 MHz CL9 | HDD: Muskin Chronos Deluxe 240GB(win8) && ADATA SX900 120 GB(ubuntu 12.04) | PSU: Seasonic x760 |

Link to comment
https://linustechtips.com/topic/219959-c-assistance/#findComment-3038207
Share on other sites

Link to post
Share on other sites

 

You don't need to reverse it like that.

class A{	private:		int s;	public:		A(int i) : s(i) {}		~A(){}		friend std::ostream& operator<<(std::ostream& out, const A& a)		{			out << a.s << std::endl;			return out;		}};int main(){	A a(10);	A a2(20);	std::cout << a << a2;	return 0;}

prints:

10

20

 

as expected.

1474412270.2748842

Link to comment
https://linustechtips.com/topic/219959-c-assistance/#findComment-3038362
Share on other sites

Link to post
Share on other sites

You don't need to reverse it like that.

hm that feels a bit tricky to me, because that "short" version doesn't make obvious what's going on

i think @Gob 's affirmation stands, because that function is not a class method, so i don't think i would use that "trick" just to put everything into the {braces} of the class definition

 

what's really going on is too different

// declarationstd::ostream& operator<<(std::ostream&, const A&);class A{		/* class content */		// make it friend		friend std::ostream& operator<<(std::ostream& out, const A& a);};// definitionstd::ostream& operator<<(std::ostream& out, const A& a){	return out << a.s << std::endl;}

edit: i admit it's appealing though

Link to comment
https://linustechtips.com/topic/219959-c-assistance/#findComment-3038820
Share on other sites

Link to post
Share on other sites

hm that feels a bit tricky to me, because that "short" version doesn't make obvious what's going on

i think @Gob 's affirmation stands, because that function is not a class method, so i don't think i would use that "trick" just to put everything into the {braces} of the class definition

 

Fair enough, though to me it just kind of makes sense to put it in the class. I guess I just don't like random functions laying around.

1474412270.2748842

Link to comment
https://linustechtips.com/topic/219959-c-assistance/#findComment-3039220
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×