tags:

views:

46

answers:

2

I wana define a simple function in my program so I added this prototype in .h file:

      double TimeCalculation (Ptr <Node> mynode,Ptr <Node> nb_node, Ptr <Ipv4> nbipv4, Ptr <Ipv4> myipv4 );

then i wrote this function:

    double
    TimeCalculation (Ptr <Node> mynode, Ptr <Node> nb_node, Ptr <Ipv4> nbipv4, Ptr <Ipv4> myipv4)
    {
                float cpx,expx,myexpx;
                float cpy,expy,myexpy;
                float cpz,expz,myexpz;
                Vector3D cPosition;
                double itspeed;
    /////////// finding current position and exposition for my current neighbor
                cPosition = nbipv4->GetObject<MobilityModel> ()->GetPosition ();
                cpx = nbipv4->GetObject<MobilityModel> ()->GetPosition ().x;
                cpy = nbipv4->GetObject<MobilityModel> ()->GetPosition ().y;
                cpz = nbipv4->GetObject<MobilityModel> ()->GetPosition ().z;
                expx = nb_node->exPosition.at(0);
                expy = nb_node->exPosition.at(1);
                expz = nb_node->exPosition.at(2);
                itspeed = nbipv4->GetObject<ConstantSpeedPropagationDelayModel> ()->GetSpeed ();
                Vector3D normit(0.0,0.0,0.0),mycPosition;
               //Ptr <Node> mynode;
               // Ptr <Node> nb_node;  
                //v1
                //Vector myexposition = Ipv4->GetObject<Node> ()->GetmyPosition ();
                float normitx= cpx - expx;
                float normity= cpy - expy;
                float normitz= cpz - expz; 
                float itVnorm = sqrt((normitx * normitx) + (normity * normity)+(normitz * normitz)); 

                //hamide:update the amount of exposition to current position
                //pos.insert( it2 ,cPosition); 
                nb_node->exPosition.at(0) = cPosition.x;
                nb_node->exPosition.at(1) = cPosition.y;
                nb_node->exPosition.at(2) = cPosition.z;
                //for calculating v2
                //finding exposition for my node which is calculating MPR set
                myexpx = mynode->exPosition.at(0);
                myexpy = mynode->exPosition.at(1);
                myexpz = mynode->exPosition.at(2);
                mycPosition.x = myipv4->GetObject<MobilityModel> ()->GetPosition ().x; 
                mycPosition.y = myipv4->GetObject<MobilityModel> ()->GetPosition ().y; 
                mycPosition.z = myipv4->GetObject<MobilityModel> ()->GetPosition ().z; 
                double mycpx = myipv4->GetObject<MobilityModel> ()->GetPosition ().x;
                double mycpy = myipv4->GetObject<MobilityModel> ()->GetPosition ().y;
                double mycpz = myipv4->GetObject<MobilityModel> ()->GetPosition ().z;         
                //Vector norm(0.0,0.0,0.0);
                double normx= mycpx - myexpx;
                double normy= mycpy - myexpy;
                double normz= mycpz - myexpz;
                //myexPosition.at(1) = mycPosition.at(1);
                //myexPosition.at(2) = mycPosition.at(2);
                //myexPosition.at(3) = mycPosition.at(3); 
                mynode->exPosition.at(0) = mycPosition.x;
                mynode->exPosition.at(1) = mycPosition.y;
                mynode->exPosition.at(2) = mycPosition.z;          
                double myVnorm = sqrt((normx * normx)+(normy * normy)+(normz * normz));

                //v1*v2
                float entmulti = ((normitx * normx) + (normity * normy) + (normitz * normz));
                // calculating cos for the angel between two vectors for two nodes
                float cos = (entmulti /(itVnorm * myVnorm));
                //the maximum distance a node can see its neighbor its based on the 
                //settings we choose for our topology and simulation condition
                int R = 220;
                //v is the speed of each node
                double myspeed = myipv4->GetObject<ConstantSpeedPropagationDelayModel> ()->GetSpeed ();

                double Td = (R * R)/((myspeed *myspeed)+(itspeed * itspeed)-(2 * itspeed * myspeed*cos));
                return sqrt(Td);

    }

but when i run my program It has this error:

debug/libns3.so: undefined reference to `ns3::olsr::RoutingProtocol::TimeCalculation(ns3::Ptr<ns3::Node>, ns3::Ptr<ns3::Node>, ns3::Ptr<ns3::Ipv4>, ns3::Ptr<ns3::Ipv4>)'
collect2: ld returned 1 exit status

and also IT has a problem which it doesn't work perfect, I will be totally thankful if someone help me with this error and also if u find any other problem in my code.

A: 

In general, you cannot define function-template bodies in source files if you want them to be used by other source files. The compiler needs to be able to see the template definition in order to instantiate the code.

The solution is to define the function-template body in the header file.

Oli Charlesworth
I defined it in .h .did u mean .h file?
bahar
my code is in olrs.cc and my prototype in olsr.h file
bahar
@bahar: Yes, the problem is that function-templates are not functions. They don't exist until you use them; they must be visible to any code that uses them. The usual solution is to put the function-template body into the header file. See http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13.
Oli Charlesworth
thanks Oli I will try it
bahar
I tried them and my only problem was adding RoutingProtocol::Timecalculation thanks for your help
bahar
@bahar: If this solved your problem, then please accept it as the answer!
Oli Charlesworth
A: 

Apparently you forgot to link some library binaries. With gcc, you can specify libraries to link using the -l-option.

Space_C0wb0y
thanks but i don't have any idea about this binaries, do you have?
bahar
@bahar: Your error message suggests that you are using some part of the [ns-3 network simulator](http://www.nsnam.org/). You have to link to this library if you want to use functions from it. If you do not know what linking is, [read this](http://en.wikipedia.org/wiki/Linker_%28computing%29).
Space_C0wb0y
thanks, yes I'm working in ns-3 and I wanted to know how I can find the library I must link to?
bahar