English 中文(简体)
简单函数问题
原标题:problem with simple function
  • 时间:2010-10-22 09:52:17
  •  标签:
  • c++

我想在我的程序中定义一个简单的函数,所以我在.h文件中添加了这个原型:

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

然后我写了这个函数:

    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);

    }

但当我运行我的程序时,它出现了以下错误:

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

而且它也有一个问题,它不能完美地工作,如果有人帮我解决这个错误,如果你在我的代码中发现任何其他问题,我将非常感谢。

最佳回答

通常,如果希望其他源文件使用函数模板体,则不能在源文件中定义它们。编译器需要能够看到模板定义,以便实例化代码。

解决方案是在头文件中定义函数模板主体。

问题回答

显然你忘了链接一些库二进制文件。使用gcc,您可以使用-l-选项





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签