相关疑难解决方法(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++中使用向量时,Push_back导致错误

我在编译这段代码时遇到了问题.我在OS X 10.6上使用Eclipse进行编译.问题似乎只在使用向量时才会发生.我似乎根本无法使用push_back函数.每次我尝试,我得到错误"预期构造函数,析构函数或类型转换之前'.' 令牌".以下是我的代码的几个片段:

#include <GLUT/glut.h>
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <math.h>
using namespace std;
enum Colour {BLACK =0, RED=1, BLUE=2, GREEN=3, PURPLE=4, ORANGE=5, CYAN=6, BLANK=7};

class Point {
private:
    GLfloat xval, yval;
public:
    Point(float x =0.0, float y = 0.0){
        xval=x;
        yval=y;
    }

    GLfloat x() {return xval;}
    GLfloat y() {return yval;}
};


class LinePoint {
private:
    Point p;
    Colour cNum;
public:
    LinePoint(Point pnt = Point(0,0), Colour c = BLACK){
        cNum = c;
        p = pnt;
    }
    Point …
Run Code Online (Sandbox Code Playgroud)

c++ vector

4
推荐指数
1
解决办法
1948
查看次数

标签 统计

c++ ×2

vector ×1