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:

#ifndef FAISCEAU_H
#define FAISCEAU_H
#include <vector>
#include <iostream>
#include "photon.h"

class photon;
 
class faisceau
{
    public:
        void initialise(long long tps);
        faisceau copie();
        void affichefaisceau();
    private:
        std::vector<photon> tab_photon;
        
};



#endif

and faisceau.cpp:

#ifndef FAISCEAU_CPP
#define FAISCEAU_CPP
#include <vector>
#include <iostream>
#include "faisceau.h"
#include "photon.h"


void faisceau::initialise(long long tps)
        { 
            photon photon_lu(tps,0);
            tab_photon.push_back(photon_lu);
        }

void faisceau::affichefaisceau()
        {
            std::vector<photon>::iterator iter;
            for(iter=((*this).tab_photon).begin(); iter!=((*this).tab_photon).end();iter++)
            {
                cout << *iter << "\n";

            } 
        }

faisceau faisceau::copie()
        {
            return(*this);
        }

#endif

I'm getting this error line :

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> >]()’

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.

Seems that operator << is unable to take a 'photon' as input.
You probably need to write something like:

std::ostream & operator << (std::ostream& s, const photon & p)
{
    // stuff the content of the photon into the stream ...
    //	s << p.abc << etc ...
    return s;
}

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

cout << *iter.ponderation << "\n";

But it's still giving me an error.

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:

#ifndef PHOTON_H
#define PHOTON_H

using namespace std;

class photon
{
    friend class faisceau;
    public:
        photon(long long tps,int pond);
    private:  
        long long tps_arrive;
        int ponderation;
       
};

#endif

Try rather ..

cout << [B]([/B]*iter[B])[/B].ponderation << "\n";

I already did,
this way it gives this error :

/usr/lib/gcc/x86_64-linux-gnu/4.1.3/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
/tmp/cczolmYC.o: In function `faisceau::initialise(long long)':
faisceau.cpp:(.text+0x126): undefined reference to `photon::photon(long long, int)'
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

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?

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 ?

Have you actually ever succeeded in compiling/linking and running the program?

It did compile some time ago, but now I changed a lot of things in it.
Deleting the definition of the member function :

void faisceau::affichefaisceau()
        {
            std::vector<photon>::iterator iter;
            for(iter=((*this).tab_photon).begin(); iter!=((*this).tab_photon).end();iter++)
            {
               cout << (*iter).ponderation << "\n";

            } 
        }

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

I'm actually not too familiar with g++, however after looking into google ...
one reason for "undefined reference to `main'" might be that:

you compiled a .cpp that does not contain main( ), but forgot to use the -c switch When compiling .cpp files that do not contain main(), use the command line
g++ -ansi -Wall -c myfile.cpp.

Will this solve anything?

wow, a lot of thanks, I had edited my last reply saying that I'll try to get this right on my own, but you googled and answered. Thank you!
And well that's true, I was trying to compile faisceau.cpp alone, because the error was there.
now I added main.cpp, and the error is now talking about undefined reference :

/tmp/cckLxfRz.o: In function `faisceau::initialise(long long)':
faisceau.cpp:(.text+0x126): undefined reference to `photon::photon(long long, int)'
collect2: ld returned 1 exit status

I don't know why. I think I declared the photon constructor correctly:

#ifndef PHOTON_H
#define PHOTON_H

using namespace std;

class photon
{
    friend class faisceau;
    public:
        photon(long long ,int );
    private:  
        long long tps_arrive;
        int ponderation;
       
};

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

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

#endif

This seems a bit more common error, the linker is now unable to find the photon's constructor, that's called from faisceau::initialize ...
Are you sure that you are also linking the photon.cpp's object file?

Awesome!
Still, I'm so a looser. First I forgot to compile the main.cpp with faisceau.cpp, and now I did not add photon.cpp.
It's all set! THANK YOU mitrmkar. Trying to Solve this problem took all my day, and you solved it in minutes!

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.