本文介绍了当在C ++中使用向量时,Push_back导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有问题得到这段代码编译。我在OS X 10.6上用Eclipse编译。问题似乎只发生在使用向量。我似乎不能使用push_back函数。每次我尝试,我得到错误预期构造函数,析构函数或类型转换之前'。'令牌。以下是我的代码的几个代码段:

I am having issues getting this piece of code to compile. I am compiling with Eclipse on OS X 10.6. The problem seems to occur only when using vectors. I cannot seem to use the push_back function at all. Every time I try, I get the error "expected constructor, destructor, or type conversion before '.' token". Here are a few snippets of my code:

#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 getPoint(){return p;}
    Colour getColour(){return cNum;}
};
float turtleScale = 20;
Point turtlePos = Point(300./turtleScale,200./turtleScale);
LinePoint* lp = new LinePoint(turtlePos,BLACK);

vector<LinePoint*> lines;

lines.push_back(lp);

我不知道这是否与Eclipse的设置有关,如果我使用位于的代码,代替我的向量调用,它仍然

I'm not sure if this would have anything to do with how Eclipse is setup but it also seems that if I use the code located here, in place of my vector calls, it still compiles with the same error.

推荐答案

这里:

float turtleScale = 20;
Point turtlePos = Point(300./turtleScale,200./turtleScale);
LinePoint* lp = new LinePoint(turtlePos,BLACK);

vector<LinePoint*> lines;

...您使用初始化,但是:

... you use initializations, but this:

lines.push_back(lp);

...是一个语句!它必须存在于函数中:)

... is a statement! It must live in a function :)

int main()
{
    lines.push_back(lp);
}

...将会工作。

这篇关于当在C ++中使用向量时,Push_back导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 03:00