2011-01-20 184 views
1

我正在通过Accelerated C++的方式工作,并决定解决那里定义的结构之一。虽然这样做,但我遇到了一个问题:创建这些结构的向量并修改每个结构中的元素似乎都会修改其中的所有元素。我知道这可能意味着我已经将向量中的所有结构初始化为单个内存地址处的结构,但我使用.push_back()方法将“虚拟”结构插入到向量中。我的印象是.push_back()推入了它的参数副本,有效地创建了一个新的结构体。C++:为什么我的向量结构作为一个结构?

下面是结构的标题:

#ifndef _STUDENT_INFO__CHAPTER_9_H 
#define _STUDENT_INFO__CHAPTER_9_H 

#include <string> 
#include <iostream> 
#include <vector> 

class Student_info9{ 
public: 
    Student_info9(){homework = new std::vector<double>;}; 
    Student_info9(std::istream& is); 

    std::string getName() const {return name;}; 
    double getMidterm() const {return midterm;}; 
    double getFinal() const {return final;}; 
    char getPassFail() const {return passFail;}; 

    std::vector<double> *getHw(){return homework;}; 

    void setName(std::string n) {name = n;}; 
    void setMidterm(double m) {midterm = m;}; 
    void setFinal(double f) {final = f;}; 


private: 
    std::string name; 
    double midterm; 
    double final; 
    char passFail; 

    std::vector<double> *homework; 
}; 


#endif /* _STUDENT_INFO__CHAPTER_9_H */ 

这里是我与(原谅过度print语句打打闹闹的代码......一段时间尝试调试结果:) ):

vector<Student_info9> did9, didnt9; 

bool did_all_hw9(Student_info9& s) 
{ 
    vector<double>::const_iterator beginCpy = s.getHw()->begin(); 
    vector<double>::const_iterator endCpy = s.getHw()->end(); 
    return(find(beginCpy, endCpy, 0) == s.getHw()->end()); 
} 

void fill_did_and_didnt9(vector<Student_info9> allRecords) 
{ 
    vector<Student_info9>::iterator firstDidnt = partition(allRecords.begin(), allRecords.end(), did_all_hw9); 


    vector<Student_info9> didcpy(allRecords.begin(), firstDidnt); 


    did9 = didcpy; 

    vector<Student_info9> didntcpy(firstDidnt, allRecords.end()); 
    didnt9 = didntcpy; 


} 

int main(int argc, char** argv) { 

    vector<Student_info9> students; 

    Student_info9 record; 

    for(int i = 0; i < 5; i++) 
    { 
     students.push_back(record); 
    } 

    for(int i = 0; i < students.size(); i++) 
    { 
     students[i].setMidterm(85); 
     students[i].setFinal(90); 

     students[i].getHw()->push_back(90); 
     std::cout << "student[" << i << "]'s homework vector size is " << students[i].getHw()->size() << std::endl; 
     students[i].getHw()->push_back(80); 
     std::cout << "student[" << i << "]'s homework vector size is " << students[i].getHw()->size() << std::endl; 
     students[i].getHw()->push_back(70); 
     std::cout << "student[" << i << "]'s homework vector size is " << students[i].getHw()->size() << std::endl; 

     std::cout << "Just pushed back students[" << i << "]'s homework grades" << std::endl; 

     if(i == 3) 
      students[i].getHw()->push_back(0); 
    } 

    std::cout << "student[3]'s homework vector size is " << students[3].getHw()->size() << std::endl; 

    for(vector<double>::const_iterator it = students[3].getHw()->begin(); it != students[3].getHw()->end(); it++) 
     std::cout << *it << " "; 

    std::cout << std::endl; 

    std::cout << "students[3] has " << ((find(students[3].getHw()->begin(),students[3].getHw()->end(), 0) != students[3].getHw()->end()) ? "atleast one " : "no ") 
      << "homework with a grade of 0" << std::endl; 

    fill_did_and_didnt9(students); 


    std::cout << "did9's size is: " << did9.size() << std::endl; 
    std::cout << "didnt9's size is: " << didnt9.size() << std::endl; 

} 

正如你可以打印报表看,似乎功课成绩被添加到只有一个Student_info9对象,其副本似乎填充整个矢量。我的印象是,如果您要在单个对象上使用连续的.push_back()副本,则会创建该对象的副本,每个副本具有不同的内存地址。

我不确定这是否是问题的根源,但希望有人能指出我正确的方向。

谢谢。

+0

顺便说一下,这些标头警卫[非法](http:// stackoverflow。COM /问题/ 228783 /什么,是最规则有关,使用安下划线-IN-A-C-标识符)。 – GManNickG 2011-01-20 17:48:47

回答

4

当您将StudentInfo推入矢量时,它确实被复制,所以这不是问题。问题是包含作业成绩的媒介。由于您只在StudentInfo中存储指向该向量的指针,因此在复制StudentInfo时,只会复制指针而不是向量。换句话说,你有许多不同的StudentInfos,它们都有一个指向相同作业向量的指针。

要解决这个问题,你应该定义一个复制构造函数,它负责复制作业矢量。

+0

好吧,我不是创建一个复制构造函数,而是解除了“new”语句返回的内容,并使作业矢量成为一个实际的矢量(而不是一个指针)。我还改变了getHw()返回一个向量而不是一个指针,并将相应的“ - >”运算符更改为“。”。 main()中的运算符。现在的问题是没有任何家庭作业成绩被添加到Student_info9作业载体中!我认为默认的复制构造函数复制每个字段,所以我不知道为什么这不起作用 – Kevin 2011-01-20 15:43:05

+0

@Kevin:1.如果你不需要指针,你不应该使用`new`。 2.除非你让`getHw`方法返回一个指针或引用,否则它会返回一个向量的副本。所以,如果你做'students [i] .getHw()。push_back(0)``你给`getHw`返回的副本加0,而不是`student [i]`的作业向量。 – sepp2k 2011-01-20 15:48:01

2

您是否了解了复制构造函数?如果是这样,请考虑在push_back()vector<Student_info9> students发生了什么。

具体来说,这个指针会发生什么。

std::vector<double> *homework; 
1

Student_info9 record;使用第一个构造函数构造一个Student_info9。这第一个构造函数创建一个向量并将其指针存储为成员变量。然后继续将这个Student_info9的副本添加到一个向量中5次。每个副本都有一个指向同一个向量的指针。

1

StudentInfo9类contanis一个指向std::vector<double>,这意味着在默认的拷贝构造函数(当您添加StudentInfo9对象的载体,其将被称为),指针本身被复制。这意味着所有的StudentInfo9对象都有相同的作业矢量。

这有道理吗?请参阅http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/CLASSES-PTRS.html以获得更深入的指针和复制构造函数。