我有一个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) 我在编译这段代码时遇到了问题.我在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)