相关疑难解决方法(0)

如何在将对象添加到向量中时创建对象?

我有一个C++向量.我希望向量包含可变数量的对象.

Visual Studio 2012给出了一个错误:

Error: type name is not allowed
Run Code Online (Sandbox Code Playgroud)

从这个C++代码:

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

class testObject{
private:
   int someInt;
public:
   testObject(int a){ someInt=a; }
   void show() { cout<<someInt<<endl; }
};

int main()
{
    vector<testObject> testVector;
    cout << "Initial size: " << testVector.size() <<endl;

    for ( int i = 0; i < 3; i++ )
        testVector.push_back(testObject(3));
    cout << "New size: " << testVector.size() << endl;

    for ( int j = 0; j < 3; j++ )
        testVector[ …
Run Code Online (Sandbox Code Playgroud)

c++

48
推荐指数
2
解决办法
16万
查看次数

标签 统计

c++ ×1