如何将struct传递给c ++中的类?

时间:2014-06-20 10:59:20

标签: c++ class struct

我得到了这些结果:

constructor 
setFunc 
setFunc 

基本上我想要我的class-object全局并将struct数组传递给该类的setMethod。和程序正在成功编译但没有得到任何结果。

DataInput.h

#ifndef DATAINPUT_H_
#define DATAINPUT_H_

#include <stdio.h>

struct Data{
    const char *name;
    int salary;
    void set(int salary, const char *name){
        printf("setFunc \n");
        this->name = name;
        this->salary = salary;
    }
};

此类位于具有上述头文件

的单独cpp文件中
class DataInput {
public:
    int dataSize;
    struct Data data[];

    DataInput();
    virtual ~DataInput();

    void setData(struct Data d[], int numberOfData);
    void printData();

private:

};

#endif

------- EOF ---------- DataInput.cpp

#include "DataInput.h"

DataInput::DataInput() {
    printf("constructor \n");
    dataSize = 0;
}

DataInput::~DataInput() {

}

void DataInput :: setData(struct Data d[], int numberOfData){
    dataSize = numberOfData;
    for (int i = 0; i< numberOfData; i++){
        printf("i-val in setData() --> %d",i);
        this->data[i] = data[i];
    }
}

void DataInput::printData(){
    for (int i = 0; i< dataSize; i++){
        printf("name--> %s \n",data[i].name);
        printf("salary--> %d \n",data[i].salary);
    }
}

-------- EOF ----------- main.cpp中

#include <stdlib.h>
#include "DataInput.h"
#include <stdio.h>


DataInput *dataInput;
int main(void) {
    DataInput in;
    dataInput = &in;

    struct Data d[2];

    d[0].set(1000, "ABC");
    d[1].set(2000, "XYZ");

    dataInput->setData(d, 2); //not setting data
    dataInput->printData(); //not printing
    return 0;
}

2 个答案:

答案 0 :(得分:1)

注意:可能无法编译,只是说明性的

少数事情:

DataInput构造函数不为新项目保留空间。因此,this->data[i] = data[i];结果未定义。

这是C ++,三个规则,字符串...... ....

struct Data
{
    std::string name;
    int salary;

    Data(const std::string & n, int s);
    Data & operator=(const Data & d);
};

Data::Data(const std::string & n, int s) :
    name(n), salary(s)
{
}

Data & Data::operator=(const Data & d)
{
    name = d.name;
    salary = d.salary;
    return *this;
}

使用标准容器:

class DataInput 
{
    private:
        std::vector<Data> data;

    public:
        DataInput();
        virtual ~DataInput();

        // you don't need use size
        void setData(const std::vector<Data> & d);
        void printData();
};

void DataInput::setData(const std::vector<Data> & d)
{
    data = d;
}

void DataInput::printData()
{
    for (std::vector<Data>::iterator it = data.begin(); it != data.end(); ++it)
    {
        std::cout << it->name << ":" << it->salary << std::endl;
    }
}

现在,您可以从main(无指针)使用它:

int main(void) 
{
    DataInput in;

    std::vector<Data> d;

    d.push_back(Data(1000, "ABC"));
    d.push_back(Data(2000, "XYZ"));

    dataInput.setData(d); // yes setting data
    dataInput.printData(); // yes printing
    return 0;
}

答案 1 :(得分:0)

执行整个数组的memcpy,只是分配数据可能会因堆栈帧的删除或删除而导致内存泄漏。这样一来,你就会失去记录同时避免for循环提高性能

void DataInput :: setData(const struct Data const* d, const int numberOfData) {
      // Cleanup old data!
      if(this->data) delete [] this->data;

      if(!d) throw new std::invalid_argument("Cannot set NULL data!"); // Remove this line if NULL may be assigned and replace with the commented code.
      // if(!d) {
      //     this->data = NULL;
      //     this->dataSize = 0;
      // } else {
      this->data = new Data[(this->dataSize = numberOfData)];
      memcpy(this->data, d, sizeof(struct Data) * numberOfData);
      // }
}

不要忘记更新DataInput类!

class DataInput {
    public:
        int dataSize;
        struct Data* data;

        DataInput();
        virtual ~DataInput();

        void setData(const struct Data const* d, const int numberOfData);
        void printData();

    private:
};

void DataInput::printData() {
    for (int i = 0; i< this->dataSize; i++){
        printf("name--> %s \n",this->data[i].name);
        printf("salary--> %d \n",this->data[i].salary);
    }
}
DataInput::~DataInput() {
   if(this->data) delete [] this->data;
}