Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

Declaring with a vector as a class member [C++]

I have a problem with declaring a vector of pointers to objects I created. I have two Base classes, let it be class A and B. I also have C and D derived from A. In B.h I have:

#ifndef B_H
#define B_H
#include "A.h"
#include <cstdlib>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

class B
{
    unsigned int k;
    unsigned int m;
    unsigned int n;
    vector < C* > cs;
    vector < D* > ds;
    string** t;

    public:
    B();
    B(string nameoffile);
    ~B();
};

#endif // B_H

Now I would like to add some things to these vectors in the constructor. But there appears to be a problem. When I try to use them they are somehow not found. I have a code in B.cpp like:

B:: B(string nameoffile)
{
        ....
            cs.push_back(new C(...)); // new Constructor of C
        ....
}

The program does not work in the line above. There are no errors nor warnings, but the program stops here. It seems like I can not use these vectors, beacuse they are not found or something. There is no problem with the code, beacuse I tried to use it with completely new vectors created inside the constructor and it worked. But it does not work with the vectors as members of B. I also tried to clear the vectors in the beginning, but it did not help. No operations on these vectors are possible.

question from:https://stackoverflow.com/questions/66055819/declaring-with-a-vector-as-a-class-member-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...