0

我还是 C++ 的新手。我想阅读来自多个来源的消息。每个源将以 4 字符 ID 开始数据消息。每个也将有几个数据消息。没有一条消息包含我想要从设备获得的所有信息。那么如果我创建一个ID为对象名的对象,下次收到消息时,该对象会更新还是完全重构呢?有没有办法在代码中调用它之前检查对象是否已经构造?

  class Channels{
public:
    INT8U systemID;    //0x01 Glonass, 0x02 GPS
    INT8U satID;
    INT8U GlonassNumber;
    INT8U SNR;         //signal to noise ratio
    FP64 carrierPhase; //cylces
    FP64 psuedoRange;  //milliseconds
    FP64 doppler;      //HZ cycles
    float tropDelay; //meters
    float ionoDelay; //meters
};
class BaseStation{
public:
    Channels channel[32];   //each channel object has all channel class variables in it
    int numberSatelitesTracked;
    FP64 timeUTC;
    INT16U week;
    FP64 GPStoUTCoffset;
    FP64 GLOtoUTCoffset;
    INT8S recieverTimeOffset;
    FP64 posX;   //geocentric coordinates in meters
    FP64 posY;
    FP64 posZ;
    FP64 rmsX;   //expected root mean square error of coordinates
    FP64 rmsY;
    FP64 rmsZ;
};
 if( check == SOCKET_ERROR){
            if( WSAGetLastError() != WSAEWOULDBLOCK){
                printf("base station client recieve failed with error %d \n",         WSAGetLastError());
                FreeSocketInformation(i); //shuts down client socket if no data
            }
            continue;
        }
        else{
            //recieve bytes into array
            memcpy(recvArray, SocketInfo->DataBuf.buf, SocketInfo->RecvBytes +1);
            //print recieved bytes on screen
            printf("%s \n", SocketInfo->DataBuf.buf);

        //first 4 bytes in message are base ID
            cBuffer[0] = recvArray[0];
            cBuffer[1] = recvArray[1];
            cBuffer[2] = recvArray[2];
            cBuffer[3] = recvArray[3];
            baseID = cBuffer;
        //create object with 4 char name
            BaseStation baseID;

        //test message identity and sort data
            if(recvArray[4] == 0x10 && recvArray[5] == 0xF5){
                baseID.timeUTC = combine64(recvArray[6]);
                baseID.week = combine16u(recvArray[14]);
                baseID.GPStoUTCoffset = combine64(recvArray[16]);
                baseID.GLOtoUTCoffset = combine64(recvArray[24]);
                baseID.recieverTimeOffset = recvArray[32];
                int noChannels = (check-30) /30 ;
                if (noChannels >= 32){
                    noChannels = 32;
                }
                int x = 33;
                for(int m = 0; m < noChannels; m++){   //advance reading for channel m

                    baseID.channel[m].systemID = recvArray[x];
                    x++;
                    baseID.channel[m].satID = recvArray[x];
                    x++;
                    baseID.channel[m].GlonassNumber = recvArray[x];
                    x++;
                    baseID.channel[m].SNR = recvArray[x];
                    x++;
                    baseID.channel[m].carrierPhase = combine64(recvArray[x]);
                    x = x+8;
                    baseID.channel[m].psuedoRange = combine64(recvArray[x]);
                    x = x+8;
                    baseID.channel[m].doppler = combine64(recvArray[x]);
                    x = x+10;
                }  //end of for loop to gather F5 sat data
            }  //end F5 message data


            if(recvArray[4] == 0x10 && recvArray[5] == 0xF6){
                baseID.posX = combine64(recvArray[6]);
                baseID.posY = combine64(recvArray[14]);
                baseID.posZ = combine64(recvArray[22]);
                baseID.rmsX = combine64(recvArray[30]);
                baseID.rmsY = combine64(recvArray[38]);
                baseID.rmsZ = combine64(recvArray[46]);
            } //end F6 message data

好的,所以看起来数组可能是我最好使用的。因此,如果我设置 100 个基础对象,然后使用第二个布尔数组跟踪活动数组元素,这看起来应该有效吗?(baseID 添加到基础对象)

BaseStation base[100];
boolean baseActive[100];
int baseNumber;
//begin message processing------------------------------------------------------------
        //first 4 bytes in message are base ID
            cBuffer[0] = recvArray[0];
            cBuffer[1] = recvArray[1];
            cBuffer[2] = recvArray[2];
            cBuffer[3] = recvArray[3];
            string name = cBuffer;
//check for existing baseID------------------------------------------------------------
// 100 array positions
//find if base is already in use, create new if not in use
        for(baseNumber = 0; base[baseNumber].baseID != name; baseNumber++){
            //for statement increases untill it finds baseID == name
            if( baseNumber >= 100){ //baseID not currently in use
                for(int n=0; baseActive[n] == true; n++){
                    //for statement increases untill finds a false baseActive
                    baseNumber = n; //assign baseNumber to the array position
                    base[baseNumber].baseID = name; //create new baseID
                    continue;
                }
            }
        }
//check and process message data--------------------------------------------------------
        if( base[baseNumber].baseID == name){
            baseActive[baseNumber] = true;
            //test message identity and sort data
           }//end of for loop

//test connection, if no bytes recieved then connection is closed.----------------------
            if( SocketInfo->RecvBytes == 0){
                FreeSocketInformation(i); //shuts down client socket if no data
                continue;
            }
        }
    } //end of read data from socket
}

//need to add a timer to remove non sending bases from the baseActive[] array
4

2 回答 2

3

C++ 是一种静态类型语言,您需要在编译时提供对象名称。
您不能在运行时创建对象名称并使用该名称创建对象。

于 2012-12-20T04:24:56.907 回答
1

正如已经回答的那样,您不能在 C++ 中这样做。但是,您可以通过其他方式解决您的问题。首先,您需要将一些 ID 绑定到结构 BaseStation 的一些具体对象。您可以通过两种方式提供此链接 - 通过将 BaseStation 对象保存在关联容器中,其中键是 ID,或者通过保存 BaseStation 对象数组(据我所知,您正在编写某种微控制器代码,因此 std 容器不能可供您使用)。

第一种方法代码示例:

//id is 4 char so it can be thought as int on most systems
std::map<int, BaseStation *> baseStations;
int * id = (int*)recvArray; //this hack is for showing how you can convert 4 char to int
    //may be in your code (int id = combine32(recvArray[0])) is equvivalent
if(baseStations.find(*id) != baseStations.end()) //checking existance of object with such id
{
    //ok, exists, do nothing
}
else
    baseStations[*id] = new BaseStation(); //create new

baseStations[*id].timeUTC = combine64(recvArray[6]); //starting copying values
//other values copying

在第二种情况下,如果您不能使用关联容器或由于微控制器内存不足而买不起它们的库\代码,您可以只使用数组,但它根本不灵活并且消耗更多操作。例子:

//BaseConnection also holds field names id;
BaseConnection baseConnections[N];
int FindId(int id); //return index of element in baseConnections array with this id

BaseConnection * workingConnection = &baseConnections[FindId(combine32(recvArray[0]))];
workingConnection->timeUTC = combine64(recvArray[6]); //starting copying values
//other values copying
于 2012-12-20T04:33:21.640 回答