仅在代码的一部分中对xxx的未定义引用

时间:2015-04-16 15:29:26

标签: c++11 linker

我仅在特定功能中链接问题。 我有一个由类ActorNode(ActorNode.cpp)和SensorNode(SensorNode.cpp)继承的基类Node(Node.h,Node.cpp),main()在单独的文件Program.cpp中。

这就是问题所在:

swar@swar-Inspiron-5520:~/swar/WSNProject/src$ g++ -std=c++11 -c Node.h Node.cpp ActorNode.cpp ChildNode.h SensorNode.cpp Program.cpp
swar@swar-Inspiron-5520:~/swar/WSNProject/src$ g++ Node.o ActorNode.o SensorNode.o Program.o
Program.o: In function `main':
Program.cpp:(.text+0x179): undefined reference to `std::vector<SensorNode, std::allocator<SensorNode> > Node::GetNodesInRange<SensorNode>(std::vector<SensorNode, std::allocator<SensorNode> >, double)'
Program.cpp:(.text+0x269): undefined reference to `void Node::Broadcast<SensorNode>(std::vector<SensorNode, std::allocator<SensorNode> >, Message, double)'
Program.cpp:(.text+0x447): undefined reference to `void Node::SendMsg<ActorNode>(Message, ActorNode)'
collect2: error: ld returned 1 exit status
swar@swar-Inspiron-5520:~/swar/WSNProject/src$ ^C

以下是代码部分:

// Program.cpp

#include "ChildNode.h"
#include <typeinfo>

template <class T>
vector<T> InitNodeArray(int count);
template <class T>
void PrintNodeArray(vector<T> NodeArray);

//Length and breadth of area
int areaX, areaY;


int main()
{   
  do{
    cout << "Enter the length and breadth of area under supervision" << endl << "X: ";
    cin >> areaX;
    cout << "Y: ";
    cin >> areaY;
  } while (typeid(areaX) != typeid(int) || typeid(areaY) != typeid(int));

  vector<ActorNode> actorNodeArray = InitNodeArray<ActorNode>(ACTOR_NODE_COUNT);

  vector<SensorNode> sensorNodeArray = InitNodeArray<SensorNode>(SENSOR_NODE_COUNT);

  //only 1 actor node
  vector<ActorNode>::iterator aIter;
  vector<SensorNode>::iterator sIter;

  //for each actor node
  for(aIter = actorNodeArray.begin(); aIter!= actorNodeArray.end(); aIter++)
    {
      //get all in range sensor nodes
      for (int iteration = 0; iteration < 3; iteration++)
    {
      aIter->SetIterRadius(iteration);
      vector<SensorNode> inRangeSNode = aIter->GetNodesInRange<SensorNode>(sensorNodeArray, ACTOR_RANGE);

      vector<SensorNode> RedSensorNodeArray;

      for (sIter = inRangeSNode.begin(); sIter != inRangeSNode.end(); sIter++)
        {
          //hi message - to know who all are in range 
          sIter->Broadcast<SensorNode>(inRangeSNode, Message({ "", sIter->id, -1, TYPE0, true }), SENSOR_NODE_RANGE);
          //set if is redundant
          sIter->isRedundant = sIter->IsRedundant();

          //send position and energy to ActorNode for registration as redundant node
          string info = "";
          info += sIter->globalPos.xCoord;
          info += ",";
          info += sIter->globalPos.yCoord;
          info += ",";
          info += sIter->currentEnergy;

          sIter->SendMsg<ActorNode>(Message({ info, sIter->id, aIter->id, TYPE2, false }), *aIter);

          RedSensorNodeArray.push_back(*sIter);
          //direct registration
          //if (sIter->isRedundant{ aIter->RegisterRedNode(*sIter);     }   
        }   

      PrintNodeArray<SensorNode>(RedSensorNodeArray);
      for (sIter = RedSensorNodeArray.begin(); sIter != RedSensorNodeArray.end(); sIter++)
        {
          aIter->InformSensorNodeForMove(*sIter, RedSensorNodeArray);
        }
    }
    }

  return 0;
}

template <class T>
vector<T> InitNodeArray(int count)
{
  vector<T> actorNodeArray(count);
  double x, y;
  T aNode;
  for (int i = 0; i < count; i++)
    {
      x = rand() % areaX;
      y = rand() % areaY;
      aNode = T(i, x, y);
      actorNodeArray.push_back(aNode);
    }
}

template <class T>
void PrintNodeArray(vector<T> NodeArray)
{
  typename vector<T>::iterator iter;
  for (iter = NodeArray.begin(); iter != NodeArray.end(); iter++)
    {
      iter->PrintNodeInfo();
    }

}

其他功能如下:

InformSensorNodeInRange()

中的

ActorNode.cpp SendMsg<>()

中的

Broadcast()GetNodesInRange()Node.cpp

ChildNode.h是ActorNode.cpp和SensorNode.cpp的头文件。

// ChildNode.h

#include <iostream>
#include <vector>
#include "Node.h"
#include <string>
#include <sstream>
#include <math.h>

#define INITIAL_ENERGY 18720
#define SENSOR_NODE_COUNT 20
#define ACTOR_NODE_COUNT 1
#define SENSORS_PER_ACTOR SENSOR_NODE_COUNT/ACTOR_NODE_COUNT
#define DIFF_ANGLE 360/SENSORS_PER_ACTOR
#define ACTOR_RANGE 300 //depends on TxPower; subject to variation
#define R 1.154 * ACTOR_RANGE
#define R3 R
#define R2 1.154 * R3
#define R1 1.154 * R2
#define PI 3.14
#define SENSOR_NODE_RANGE 50
#define SENSOR_ENERGY 9360 //subject to change 


using namespace std;

//**********SENSOR NODE**********

// Position Coordinate List
typedef struct PosCoord
{
  int x;
  int y;
  int actorID;

}PosCoord;

class SensorNode : public Node
{  

public:
  int helloCount;
  int currentEnergy;
  bool isRedundant;
  vector<PosCoord> posCoordArray;

  //Constructors
  SensorNode();
  SensorNode(int id,double x,double y);
  SensorNode(int id,Position pos);
  //methods
  void Init();
  Position FindPos(Message msg);
  Position* GetPos();
  bool IsRedundant();  
  //void ListenAndReply(Message msg, vector<ActorNode> actorNodeList);
  template <class T>
    void ReceiveMsg(Message msg, T sndNode);  
 };


//********** ACTOR NODE **************

typedef struct RedundantNode
{
  int sensorId;
  double currentEnergy;
  Position rectPos;
  Polar polarPos;
}RedundantNode;

//Changed method signatures
class ActorNode: public Node
{
 private :
  double diffAngle;
  double iterRadius; 
  //  Polar polarPos;

 public:
  //int actorId;   
  vector<RedundantNode> redNodeArray;
  //Constructors
  ActorNode();
  ActorNode(int id, double x, double y);
  ActorNode(int id, Position pos);
  ActorNode(int id, Polar polarPos, double originX, double originY);  

  //methods    
  void Init();  
template<class T>
  void ReceiveMsg(Message msg, T sndNode);  
  void RegisterRedNode(SensorNode sNode);
  void RegisterRedNode(SensorNode *sNode);
  RedundantNode* FormRedNode(int senderID, vector<int> posArray); 
  Polar CalculateRelativePos(Position actorPos, Position sensorPos);
  RedundantNode* MaxEnergyNode(vector<RedundantNode> redNodeArray);
  double GetDiffAngle();
  void SetDiffAngle(double value);
  double GetIterRadius(int iteration);
  void SetIterRadius(int iteration);
  Position CalculateFinalPos(RedundantNode redNode);
  vector<RedundantNode> NodesInDiffAngleRange(RedundantNode redNode, double diffAngle);
  vector<RedundantNode> InformSensorNodeForMove(SensorNode sNode, vector<SensorNode> sensorArray);
  void SetFinalPosForRedNodes();
  RedundantNode* GetRedNodeFromSensor(SensorNode sNode);
  SensorNode GetSensorFromRedNode(RedundantNode redNode, vector<SensorNode> sensorArray);    


};

0 个答案:

没有答案