error: no match for ‘operator<<’ in ‘std::cout

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 16
Reputation: adnanius is an unknown quantity at this point 
Solved Threads: 0
adnanius adnanius is offline Offline
Newbie Poster

error: no match for ‘operator<<’ in ‘std::cout

 
0
  #1
Mar 8th, 2008
hello
I'm new to c++ and I'm writing a program for a college project.
I'm getting an error I don't understand, if someone can help me with it I'll be thankful
Oh, and by the way, the functions names are in French, hope this wont be a problem.
Here is my code :
faisceau.h:
  1. #ifndef FAISCEAU_H
  2. #define FAISCEAU_H
  3. #include <vector>
  4. #include <iostream>
  5. #include "photon.h"
  6.  
  7. class photon;
  8.  
  9. class faisceau
  10. {
  11. public:
  12. void initialise(long long tps);
  13. faisceau copie();
  14. void affichefaisceau();
  15. private:
  16. std::vector<photon> tab_photon;
  17.  
  18. };
  19.  
  20.  
  21.  
  22. #endif

and faisceau.cpp:

  1. #ifndef FAISCEAU_CPP
  2. #define FAISCEAU_CPP
  3. #include <vector>
  4. #include <iostream>
  5. #include "faisceau.h"
  6. #include "photon.h"
  7.  
  8.  
  9. void faisceau::initialise(long long tps)
  10. {
  11. photon photon_lu(tps,0);
  12. tab_photon.push_back(photon_lu);
  13. }
  14.  
  15. void faisceau::affichefaisceau()
  16. {
  17. std::vector<photon>::iterator iter;
  18. for(iter=((*this).tab_photon).begin(); iter!=((*this).tab_photon).end();iter++)
  19. {
  20. cout << *iter << "\n";
  21.  
  22. }
  23. }
  24.  
  25. faisceau faisceau::copie()
  26. {
  27. return(*this);
  28. }
  29.  
  30. #endif

I'm getting this error line :

  1. faisceau.cpp:20: error: no match for ‘operator<<’ in ‘std::cout << iter.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = photon*, _Container = std::vector<photon, std::allocator<photon> >]()
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 62
Reputation: Joatmon is an unknown quantity at this point 
Solved Threads: 7
Joatmon Joatmon is offline Offline
Junior Poster in Training

Re: error: no match for ‘operator<<’ in ‘std::cout

 
0
  #2
Mar 8th, 2008
simply put, cout doesn't know how to print out an interator. also I am confused as to why you are attempting to dereference the iterator when it isn't a pointer.

Try converting the iterator to something you know the cout can print.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: error: no match for ‘operator<<’ in ‘std::cout

 
0
  #3
Mar 8th, 2008
Seems that operator << is unable to take a 'photon' as input.
You probably need to write something like:
  1. std::ostream & operator << (std::ostream& s, const photon & p)
  2. {
  3. // stuff the content of the photon into the stream ...
  4. // s << p.abc << etc ...
  5. return s;
  6. }
Last edited by mitrmkar; Mar 8th, 2008 at 4:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 16
Reputation: adnanius is an unknown quantity at this point 
Solved Threads: 0
adnanius adnanius is offline Offline
Newbie Poster

Re: error: no match for ‘operator<<’ in ‘std::cout

 
0
  #4
Mar 8th, 2008
you are so right! cout cant take a photon as input, horrible mistake!I didn't add the wanted element of photon.
Changed it to
  1. cout << *iter.ponderation << "\n";
But it's still giving me an error.
  1. faisceau.cpp:20: error: ‘class __gnu_cxx::__normal_iterator<photon*, std::vector<photon, std::allocator<photon> > >’ has no member named ‘ponderation’

Here's my photon.h:
  1. #ifndef PHOTON_H
  2. #define PHOTON_H
  3.  
  4. using namespace std;
  5.  
  6. class photon
  7. {
  8. friend class faisceau;
  9. public:
  10. photon(long long tps,int pond);
  11. private:
  12. long long tps_arrive;
  13. int ponderation;
  14.  
  15. };
  16.  
  17. #endif
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: error: no match for ‘operator<<’ in ‘std::cout

 
0
  #5
Mar 8th, 2008
Try rather ..
cout << (*iter).ponderation << "\n";
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 16
Reputation: adnanius is an unknown quantity at this point 
Solved Threads: 0
adnanius adnanius is offline Offline
Newbie Poster

Re: error: no match for ‘operator<<’ in ‘std::cout

 
0
  #6
Mar 8th, 2008
I already did,
this way it gives this error :
  1. /usr/lib/gcc/x86_64-linux-gnu/4.1.3/../../../../lib/crt1.o: In function `_start':
  2. (.text+0x20): undefined reference to `main'
  3. /tmp/cczolmYC.o: In function `faisceau::initialise(long long)':
  4. faisceau.cpp:(.text+0x126): undefined reference to `photon::photon(long long, int)'
  5. collect2: ld returned 1 exit status

maybe I should give you photon.cpp

#ifndef PHOTON_CPP
#define PHOTON_CPP
#include "photon.h"

photon::photon(long long tps, int pond)
{
tps_arrive=tps;
ponderation=pond;
}

#endif
Last edited by adnanius; Mar 8th, 2008 at 4:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: error: no match for ‘operator<<’ in ‘std::cout

 
0
  #7
Mar 8th, 2008
Now it seems that you have stumbled upon another problem.
This is a linker error:
collect2: ld returned 1 exit status
which means that the code compiles OK but does not link for some reason.
Are you using some IDE or do you compile and link via shell/command line?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 16
Reputation: adnanius is an unknown quantity at this point 
Solved Threads: 0
adnanius adnanius is offline Offline
Newbie Poster

Re: error: no match for ‘operator<<’ in ‘std::cout

 
0
  #8
Mar 8th, 2008
I'm compiling with a command line, g++ under linux.
I'm suspecting the friend class faisceau to be the problem.
I've read that making a class a driend of another has consequences on the #includes to put ?
Last edited by adnanius; Mar 8th, 2008 at 4:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: error: no match for ‘operator<<’ in ‘std::cout

 
0
  #9
Mar 8th, 2008
Have you actually ever succeeded in compiling/linking and running the program?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 16
Reputation: adnanius is an unknown quantity at this point 
Solved Threads: 0
adnanius adnanius is offline Offline
Newbie Poster

Re: error: no match for ‘operator<<’ in ‘std::cout

 
0
  #10
Mar 8th, 2008
It did compile some time ago, but now I changed a lot of things in it.
Deleting the definition of the member function :
  1. void faisceau::affichefaisceau()
  2. {
  3. std::vector<photon>::iterator iter;
  4. for(iter=((*this).tab_photon).begin(); iter!=((*this).tab_photon).end();iter++)
  5. {
  6. cout << (*iter).ponderation << "\n";
  7.  
  8. }
  9. }

doesn't change anything.
And even the undeclaration of the friend class
So I think the original problem is now solved .
I'll try to figure this out.
And I'll post if I never get it right!
Thanks ! It's been a pleasure getting fast and accurate answers
Last edited by adnanius; Mar 8th, 2008 at 5:04 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC