0

可能很难帮助解决这个问题,因为我有很多代码并且并不真正知道是什么弄乱了我的输出。
背景信息是我正在编写一个程序,该程序从文件中读取文本行,将这些行解析为标记,然后将标记转换为命令以制作不同类型的“传感器”(数字和模拟)。

我有两个主要问题。制作新传感器时,会创建对象并将指向该对象的指针放入数组中。当打印所有传感器的信息时,将调用数组中指向的每个对象的打印函数。这一切都很好,但由于某种原因,我的传感器的名称总是空白。正确的标记被解析,并分配给对象的正确部分,但不知何故出了问题,所有传感器名称都列为空白。

另一个问题类似。还有另一个 sensorNode 对象的指针数组。当我创建两个 sensorNode 对象并通过它们的数组调用它们的打印函数时,它们打印得很好。但是,当我向数组中添加两个 sensorNode 对象并再次调用它时,后两个的信息打印正常,而前两个的信息全是垃圾。那里发生了什么失去前两个?

这是我的代码,我知道我放了很多,但我认为比少好。

我的主要:(对象是在“新传感器”和“新节点”案例末尾的 switch 语句中创建的,并在“状态”案例中打印)

#include <iostream>
#include <fstream>
using namespace std;

#include "definitions.h"
#include "system_utilities.h"
#include "sensor.h"
#include "sensor_node.h"


int main()
{
    int val;
    ifstream inFile;
    char line[MAX_CMD_LINE_LENGTH];
    char* token[MAX_TOKENS_ON_A_LINE];
    int numtokens;

    SensorNode* NodeList[MAX_NODES];
    sensor* SensorList[MAX_SENSORS];
    int numNodes = 0;
    int numSensors = 0;

    fillSystemCommandList();

    inFile.open("p6input.txt", ios::in);
    if(inFile.fail()) {
        cout << "Could not open input file.  Program terminating.\n\n";
        return 0;
    }
    do {
    inFile.getline(line, MAX_CMD_LINE_LENGTH);
    line[strlen(line)+1] = '\0';
    numtokens = parseCommandLine(line, token);
        int t;
        cout << "Number of tokens: " << numtokens << "\n";

                val = getCommandNumber(token[0]);

            switch(val) {
             case HALT:
                    for (t=1; t <= numtokens; t++) {
                        cout << "Token "<< t << ": " << token[t-1] << "\n"; 
                    }
             cout << "Recognized command \"Halt\"";
             break;
             case STATUS:
                    for (t=1; t <= numtokens; t++) {
                        cout << "Token "<< t << ": " << token[t-1] << "\n";
                    }
                    if (strcmp(token[1], "nodes") == 0) {

                        for (int i = 0; i < numNodes; i++) {
                            NodeList[i]->print();
                        }
                    }
                    if (strcmp(token[1], "sensors") == 0) {

                        for (int j = 0; j < numSensors; j++) {
                            SensorList[j]->print();
                        }
                    }

             cout << "Recognized command \"Status\"";
             break;
             case TIME_CLICK:
                    for (t=1; t <= numtokens; t++) {
                        cout << "Token "<< t << ": " << token[t-1] << "\n";
                    }
             cout << "Recognized command \"time click\"";
             break;
             case NEW_SENSOR:
                    for (t=1; t <= numtokens; t++) {
                        cout << "Token "<< t << ": " << token[t-1] << "\n";
                    }

                    if (strcmp(token[1], "digital") == 0) {

                        SensorList[numSensors] = new digitalSensor(token[2],convertFloatToValue(token[3]));
                    }
                    if (strcmp(token[1], "analog") == 0) {

                        SensorList[numSensors] = new analogSensor(token[2],convertFloatToValue(token[3]),convertIntToValue(token[4]),convertIntToValue(token[5]));
                    }

                    numSensors++;

             cout << "Recognized command \"New Sensor\"";
             break;
             case NEW_SENSOR_NODE:
                    for (t=1; t <= numtokens; t++) {
                        cout << "Token "<< t << ": " << token[t-1] << "\n";
                    }

                NodeList[numNodes] = new SensorNode(token[1], convertFloatToValue(token[3]), convertFloatToValue(token[4]), convertFloatToValue(token[5]), convertIntToValue(token[2]), convertFloatToValue(token[6]));

                    numNodes++;
             cout << "Recognized command \"New Sensor Node\"";
             break;
             case NEW_NETWORK:
                    for (t=1; t <= numtokens; t++) {
                        cout << "Token "<< t << ": " << token[t-1] << "\n";
                    }
             cout << "Recognized command \"New Network\"";
             break;
             case ADD_SENSOR_TO_NODE:
                    for (t=1; t <= numtokens; t++) {
                        cout << "Token "<< t << ": " << token[t-1] << "\n";
                    }
             cout << "Recognized command \"Add sensor to node\"";
             break;
             case ADD_NODE_TO_NETWORK:
                    for (t=1; t <= numtokens; t++) {
                        cout << "Token "<< t << ": " << token[t-1] << "\n";
                    }
             cout << "Recognized command \"Add node to network\"";
             break;
             case SENSOR_COMMAND:
                    for (t=1; t <= numtokens; t++) {
                        cout << "Token "<< t << ": " << token[t-1] << "\n";
                    }
             cout << "Recognized command \"Sensor Command\"";
             break;
             case UNDEFINED_COMMAND:
                    for (t=1; t <= numtokens; t++) {
                        cout << "Token "<< t << ": " << token[t-1] << "\n";
                    }
              break;
             default:
             cout << "Undefined Command";
             }
        for (t=1; t <= numtokens; t++) {
            free((void *)token[t-1]);
        }
            cout << "\n";

        cout << "\n";
    }
    while (val != HALT);
    return 0;
}

这是我的传感器.h

#ifndef __Program_6__sensor__
#define __Program_6__sensor__

#include <iostream>

class sensor {
protected:
    char* SensorName;
    float energyDraw;
    int functioning;
    int onoff;

public:
    sensor(char*n, float pc);
    virtual void print();

    void setOK(int K);
    int getOK();
    void setOnOff(int n);
    int getOnOff();
};
//---------
class digitalSensor : public sensor {
    int reading;

public:
    digitalSensor(char*n, float pc);
    virtual void print();
    void setCurrentReading(int r);
    int getCurrentReading();
};

class analogSensor : public sensor {
    int Reading;
    int minRead;
    int maxRead;

public:
    analogSensor(char *n, float pc, int mn, int mx);
    virtual void print();
    void setCurrentReading(int r);
    int getCurrentReading();
};


#endif /* defined(__Program_6__sensor__) */

我的sensor.cpp的一部分:

#include "sensor.h"
#include "definitions.h"
using namespace std;

//--------SENSOR CLASS------------//
sensor::sensor(char *n, float pc) {

    SensorName = (char*)malloc(strlen(n)+1);
    energyDraw = pc;
    functioning = WORKING;
    onoff = OFF;
}
void sensor::print() {
    cout << "     Sensor: " << SensorName;
    cout << "   Power Consumption: " << energyDraw;
    if (functioning == WORKING) {
        cout << "\n     Sensor is functioning correctly\n";

        if (onoff == ON) {
        cout << "     Sensor is On\n";
    }
        if (onoff == OFF) {
        cout << "     Sensor is Off\n";
    }

    }
    if (functioning == NOTWORKING) {
        cout << "     Sensor is not functioning correctly";
    }
    }
//*********DIGITAL SENSOR**********//

digitalSensor::digitalSensor(char *n, float pc) : sensor(n, pc){
    reading = 0;
}

void digitalSensor::print() {
    sensor::print();
    if (functioning == WORKING && onoff == ON) {
        cout << "     Current sensor reading is: " << reading << "\n\n";
    }
    if (onoff == OFF) {
        cout << "     Current reading not available\n\n";
    }
}


//^^^^^^^^^^^^^ANALOG SENSOR^^^^^^^^^//

analogSensor::analogSensor(char *n, float pc, int mn, int mx) : sensor(n, pc) {
    Reading = mn;
    minRead = mn;
    maxRead = mx;
}

void analogSensor::print() {
    sensor::print();
    cout << "     Minimum Value: " << minRead << ".     Maximum Value: " << maxRead;
    if (functioning == WORKING && onoff == ON) {
        cout << "\n     Current sensor reading is: " << Reading << "\n\n";
    }
    if (functioning == WORKING && onoff == OFF) {
        cout << "\n     Current reading not available\n\n";
    }

}

您可能不需要它,但这是我的(部分)sensor_node.cpp

//Functions in SensorNode
SensorNode::SensorNode(char* n, float x, float y, float z, int i, float ah)
{
    NodeName = (char*)malloc(strlen(n)+1);
    strcpy(NodeName, n);
    Node1.setx(x);
    Node1.sety(y);
    Node1.setz(z);
    NodeID = i;
    batt = ah;
    func = 1;

}

void SensorNode::print()
{

    cout << "\nSensor node " << NodeName << "\t ID: " << NodeID << "\n Location:  ";
    Node1.print();
    cout << "\n Remaining battery life: " << batt << " amp hours";
    if (func == 0) {
        cout<< "\n Node is not functioning\n";
    }
    if (func == 1) {
        cout <<"\n Node is functioning\n";

    }
}

还有我的 sensor_node.h

class SensorNode {
    char* NodeName;
    int NodeID;
    LOCATION Node1;
    float batt;
    int func;


public:
    SensorNode(char *n, float x, float y, float z, int i, float ah);
    void print();
    void setOK(int o);
    int getOK();
    void setLOC(float longi, float lat, float h);
};

最后,这是我的输出!问题出现在没有传感器显示名称的最后,并且垃圾替换了前两个节点。

输入文件:

status sensors
status nodes
new_sensor_node  "Kitchen Monitor"   41   1.5  2.5  3.5  10.0
new_sensor_node  Basement   42   3.0   4.0   5.0   5.3
new_sensor  digital  "Window open"   .2
new_sensor  digital  "Oven on"   .5
new_sensor  analog   Temperature  .03  0  255
new_sensor  analog   Humidity  3.1415926  10  20
new_sensor  digital  Alarm   2.0
status nodes
status sensors
new_sensor_node    Garage    51   87.2  93.1   0.0   20.5
new_sensor_node    Attic     52   1.1   2.2   15.3   100.75
new_sensor    digital   G1   1.5
new_sensor    digital   G2   1.5
new_sensor    digital   G3   1.5
new_sensor    digital   G4   1.5
new_sensor    digital   G5   1.5
new_sensor    digital   OOPS1   1.5
new_sensor    analog    OOPS2   1.5   0   31
status sensors
status nodes
new_sensor_node    "Last ok node"    51   87.2  93.1   0.0   20.5
new_sensor_node    "This one should not be added"     52   1.1   2.2   15.3   100.75
halt

输出:

Number of tokens: 2
Token 1: status
Token 2: sensors
Recognized command "Status"

Number of tokens: 2
Token 1: status
Token 2: nodes
Recognized command "Status"

Number of tokens: 7
Token 1: new_sensor_node
Token 2: Kitchen Monitor
Token 3: 41
Token 4: 1.5
Token 5: 2.5
Token 6: 3.5
Token 7: 10.0
Recognized command "New Sensor Node"

Number of tokens: 7
Token 1: new_sensor_node
Token 2: Basement
Token 3: 42
Token 4: 3.0
Token 5: 4.0
Token 6: 5.0
Token 7: 5.3
Recognized command "New Sensor Node"

Number of tokens: 4
Token 1: new_sensor
Token 2: digital
Token 3: Window open
Token 4: .2
Recognized command "New Sensor"

Number of tokens: 4
Token 1: new_sensor
Token 2: digital
Token 3: Oven on
Token 4: .5
Recognized command "New Sensor"

Number of tokens: 6
Token 1: new_sensor
Token 2: analog
Token 3: Temperature
Token 4: .03
Token 5: 0
Token 6: 255
Recognized command "New Sensor"

Number of tokens: 6
Token 1: new_sensor
Token 2: analog
Token 3: Humidity
Token 4: 3.1415926
Token 5: 10
Token 6: 20
Recognized command "New Sensor"

Number of tokens: 4
Token 1: new_sensor
Token 2: digital
Token 3: Alarm
Token 4: 2.0
Recognized command "New Sensor"

Number of tokens: 2
Token 1: status
Token 2: nodes

Sensor node Kitchen Monitor  ID: 41
 Location:  (LONGITUDE: 1.5 ,LATITUDE: 2.5 ,HEIGHT: 3.5 )
 Remaining battery life: 10 amp hours
 Node is functioning

Sensor node Basement     ID: 42
 Location:  (LONGITUDE: 3 ,LATITUDE: 4 ,HEIGHT: 5 )
 Remaining battery life: 5.3 amp hours
 Node is functioning
Recognized command "Status"

Number of tokens: 2
Token 1: status
Token 2: sensors
     Sensor:    Power Consumption: 0.2
     Sensor is functioning correctly
     Sensor is Off
     Current reading not available

     Sensor:    Power Consumption: 0.5
     Sensor is functioning correctly
     Sensor is Off
     Current reading not available

     Sensor:    Power Consumption: 0.03
     Sensor is functioning correctly
     Sensor is Off
     Minimum Value: 0.     Maximum Value: 255
     Current reading not available

     Sensor:    Power Consumption: 3.14159
     Sensor is functioning correctly
     Sensor is Off
     Minimum Value: 10.     Maximum Value: 20
     Current reading not available

     Sensor:    Power Consumption: 2
     Sensor is functioning correctly
     Sensor is Off
     Current reading not available

Recognized command "Status"

Number of tokens: 7
Token 1: new_sensor_node
Token 2: Garage
Token 3: 51
Token 4: 87.2
Token 5: 93.1
Token 6: 0.0
Token 7: 20.5
Recognized command "New Sensor Node"

Number of tokens: 7
Token 1: new_sensor_node
Token 2: Attic
Token 3: 52
Token 4: 1.1
Token 5: 2.2
Token 6: 15.3
Token 7: 100.75
Recognized command "New Sensor Node"

Number of tokens: 4
Token 1: new_sensor
Token 2: digital
Token 3: G1
Token 4: 1.5
Recognized command "New Sensor"

Number of tokens: 4
Token 1: new_sensor
Token 2: digital
Token 3: G2
Token 4: 1.5
Recognized command "New Sensor"

Number of tokens: 4
Token 1: new_sensor
Token 2: digital
Token 3: G3
Token 4: 1.5
Recognized command "New Sensor"

Number of tokens: 4
Token 1: new_sensor
Token 2: digital
Token 3: G4
Token 4: 1.5
Recognized command "New Sensor"

Number of tokens: 4
Token 1: new_sensor
Token 2: digital
Token 3: G5
Token 4: 1.5
Recognized command "New Sensor"

Number of tokens: 4
Token 1: new_sensor
Token 2: digital
Token 3: OOPS1
Token 4: 1.5
Recognized command "New Sensor"

Number of tokens: 6
Token 1: new_sensor
Token 2: analog
Token 3: OOPS2
Token 4: 1.5
Token 5: 0
Token 6: 31
Recognized command "New Sensor"

Number of tokens: 2
Token 1: status
Token 2: sensors
     Sensor:    Power Consumption: 0.2
     Sensor is functioning correctly
     Sensor is Off
     Current reading not available

     Sensor:    Power Consumption: 0.5
     Sensor is functioning correctly
     Sensor is Off
     Current reading not available

     Sensor:    Power Consumption: 0.03
     Sensor is functioning correctly
     Sensor is Off
     Minimum Value: 0.     Maximum Value: 255
     Current reading not available

     Sensor:    Power Consumption: 3.14159
     Sensor is functioning correctly
     Sensor is Off
     Minimum Value: 10.     Maximum Value: 20
     Current reading not available

     Sensor:    Power Consumption: 2
     Sensor is functioning correctly
     Sensor is Off
     Current reading not available

     Sensor:    Power Consumption: 1.5
     Sensor is functioning correctly
     Sensor is Off
     Current reading not available

     Sensor:    Power Consumption: 1.5
     Sensor is functioning correctly
     Sensor is Off
     Current reading not available

     Sensor:    Power Consumption: 1.5
     Sensor is functioning correctly
     Sensor is Off
     Current reading not available

     Sensor:    Power Consumption: 1.5
     Sensor is functioning correctly
     Sensor is Off
     Current reading not available

     Sensor:    Power Consumption: 1.5
     Sensor is functioning correctly
     Sensor is Off
     Current reading not available

     Sensor:    Power Consumption: 1.5
     Sensor is functioning correctly
     Sensor is Off
     Current reading not available

     Sensor:    Power Consumption: 1.5
     Sensor is functioning correctly
     Sensor is Off
     Minimum Value: 0.     Maximum Value: 31
     Current reading not available

Recognized command "Status"

Number of tokens: 2
Token 1: status
Token 2: nodes

Sensor node \200o    ID: 1064672
 Location:  (LONGITUDE: 1.4013e-45 ,LATITUDE: 1.5 ,HEIGHT: 7.00649e-45 )
 Remaining battery life: 3.50325e-44 amp hours
 Node is not functioning

Sensor node \300q    ID: 1064768
 Location:  (LONGITUDE: 1.4013e-45 ,LATITUDE: 1.5 ,HEIGHT: 7.00649e-45 )
 Remaining battery life: 3.50325e-44 amp hours
 Node is not functioning

Sensor node Garage   ID: 51
 Location:  (LONGITUDE: 87.2 ,LATITUDE: 93.1 ,HEIGHT: 0 )
 Remaining battery life: 20.5 amp hours
 Node is functioning

Sensor node Attic    ID: 52
 Location:  (LONGITUDE: 1.1 ,LATITUDE: 2.2 ,HEIGHT: 15.3 )
 Remaining battery life: 100.75 amp hours
 Node is functioning
Recognized command "Status"

Number of tokens: 7
Token 1: new_sensor_node
Token 2: Last ok node
Token 3: 51
Token 4: 87.2
Token 5: 93.1
Token 6: 0.0
Token 7: 20.5
Recognized command "New Sensor Node"

Number of tokens: 7
Token 1: new_sensor_node
Token 2: This one should not be added
Token 3: 52
Token 4: 1.1
Token 5: 2.2
Token 6: 15.3
Token 7: 100.75
Recognized command "New Sensor Node"

Number of tokens: 1
Token 1: halt
Recognized command "Halt"

一如既往,感谢您提供的任何帮助!对大量代码感到抱歉。

4

1 回答 1

0

我解决了一个问题!我添加了行 strcpy(SensorName, n); 在我的传感器初始化中的 malloc 之后。我正在分配空间,但实际上并未将参数分配给它

于 2013-05-27T23:46:07.600 回答