C++ 编译器说指针是;超出范围”;。我做错了什么?

C++ 编译器说指针是;超出范围”;。我做错了什么?,c++,class,pointers,C++,Class,Pointers,好吧,我遇到了一些不一致的情况,这让我朝着显示器上拳头大小的洞走去,我宁愿避免 我正在浏览www.sdltutorials.com上的SDL教程(我相信Tim Jones的SDL教程基础教程已经有很多人看过了),我遇到了一个错误:“Surf_Display”未在此范围内声明 这样,我想找出出错的地方,我把类似的指针代码写入了一个旧的矩形程序中,我用C++类的基础来刷新自己,用int指针看到了同样的错误,然后试图用更具体的方法来隔离这个问题。 嗯,更具体的程序编译得很好,而其他的程序则会爆炸,所以

好吧,我遇到了一些不一致的情况,这让我朝着显示器上拳头大小的洞走去,我宁愿避免

我正在浏览www.sdltutorials.com上的SDL教程(我相信Tim Jones的SDL教程基础教程已经有很多人看过了),我遇到了一个错误:“Surf_Display”未在此范围内声明

这样,我想找出出错的地方,我把类似的指针代码写入了一个旧的矩形程序中,我用C++类的基础来刷新自己,用int指针看到了同样的错误,然后试图用更具体的方法来隔离这个问题。 嗯,更具体的程序编译得很好,而其他的程序则会爆炸,所以我猜这是我缺少的一些非常基本的东西,但是注册“C++指针类”等等,什么也做不了,我不知道如何变得更具体

总之…一些代码

有效的程序

ct.h

ct.cpp

#include "ct.h"
#include <cstddef>

Classtest2::Classtest2()
{
    p_x = 0;
}

void Classtest2::set_x(int x)
{
    //do something neat here
}

int Classtest2::get_x()
{
    return *p_x;
}
编撰 g++classtest2.cpp ct.h ct.cpp-o bin/classtest2

现在…这个程序不。。。 经典

#ifndef _RECTANGLE_H
#define _RECTANGLE_H

namespace shapes
{
    class rectangle
    {
    public:
        int height;
        int width;
        int *weight;

        rectangle (int, int);
        rectangle ();
        int area ();
        void setHeight(int);
        void setWidth(int);
        void setDimensions(int, int);
    };
}
#endif
经典的cpp与重量

#include "Rectangle.h"
#include <cstddef>

using namespace shapes;

rectangle::rectangle(int h, int w)
{
    height = h;
    width = w;
    weight = NULL;
}

rectangle::rectangle()
{
    height = 0;
    width = 0;
    weight = NULL;
}

int rectangle::area ()
{
    return height * width;
}

void rectangle::setHeight(int h)
{
    height = h;
}

void rectangle::setWidth(int w)
{
    width = w;
}

void rectangle::setDimensions(int h, int w)
{
    height = h;
    width = w;
}
#包括“Rectangle.h”
#包括
使用名称空间形状;
矩形::矩形(inth,intw)
{
高度=h;
宽度=w;
权重=零;
}
矩形::矩形()
{
高度=0;
宽度=0;
权重=零;
}
int矩形::区域()
{
返回高度*宽度;
}
空矩形::设置高度(int h)
{
高度=h;
}
空矩形::设置宽度(int w)
{
宽度=w;
}
void rectangle::setDimensions(inth,intw)
{
高度=h;
宽度=w;
}
和经典的“做一些简单的事”主要

#包括“Rectangle.h”
#包括
使用名称空间std;
使用名称空间形状;
int main(int argc,char*argv[])
{
矩形tr(5,8);

您所显示的代码中可能没有错误。可能您的计算机上矩形.h中的
int*wight
声明中有输入错误,但您在此处发布了正确的代码。

矩形.cpp的正确语法如下:

#include "Rectangle.h"
#include <cstddef>

namespace shapes {

rectangle::rectangle(int h, int w)
{
    height = h;
    width = w;
    weight = NULL;
}

rectangle::rectangle()
{
    height = 0;
    width = 0;
    weight = NULL;
}

int rectangle::area ()
{
    return height * width;
}

void rectangle::setHeight(int h)
{
    height = h;
}

void rectangle::setWidth(int w)
{
    width = w;
}

void rectangle::setDimensions(int h, int w)
{
    height = h;
    width = w;
}

} // namespace shapes
#包括“Rectangle.h”
#包括
名称空间形状{
矩形::矩形(inth,intw)
{
高度=h;
宽度=w;
权重=零;
}
矩形::矩形()
{
高度=0;
宽度=0;
权重=零;
}
int矩形::区域()
{
返回高度*宽度;
}
空矩形::设置高度(int h)
{
高度=h;
}
空矩形::设置宽度(int w)
{
宽度=w;
}
void rectangle::setDimensions(inth,intw)
{
高度=h;
宽度=w;
}
}//名称空间形状

我的GCC-4.4.5和MSVC也可以使用名称空间
变量处理您的
。但根据当前标准,它是不正确的(您指定
::rectangle::area()
等,而不是
::shapes::rectangle::area()
等).

有人会说,他发布了错误的代码:)而且我确信在
int*wight
-…Cheesnope中有一个类型。没有输入错误。你在这里看到的是文件中的内容,因为我直接复制和粘贴了它们。我希望它像输入错误一样简单,但我仍然得到了答案“错误:在对Serge进行更改(和/或只是复制/粘贴他的响应)后,通过g++运行编译器输出时,未在此范围内声明:'weight'@user690598:请发布编译器版本和确切的编译器输出。您不应该试图使用g++生成命令将头文件编译为对象文件。-“g++classtest1.cpp Rectangle.cpp-o bin/classtest1"足够了你不应该为你的标题保护使用保留名称-从一开始就删除下划线。我不认为这是问题的原因,但它肯定会导致类似的问题。你应该使用初始化列表而不是ctor代码为矩形成员提供值。不过,这不是原点或y我们的问题。事实上…J T把问题解决在了鼻子上。在这两种情况下,程序无法编译都是由于预编译头的错误帮助。进行了您建议的更改。甚至将您的响应复制/粘贴到我的编辑器中,什么都没有。仍然会得到范围错误…这可能是我的本地Debian环境的问题吗?有什么问题吗lenny,这完全是配置错误,因为所有人都说它应该按编写的方式编译。但它仍然不是。@user690598:尝试将“Rectangle.h”和“Rectangle.cpp”复制到一个单独的目录中,并且只编译“Rectangle.cpp”我把它们复制到一个干净的目录中,并试图编译。嗯……没有main()入口点它不起作用。所以我也将classtest文件复制到目录中并进行了尝试。它被编译了。结果发现,在原始目录中有一个名为Rectangle.h.gch的文件。有一次我发现矩形程序被编译了!充满了新发现的希望,我跳到我的SDL教程,四处看看。CApp.h.gch!在快速的rm它编译。Boom。Headshot。那么,嗯…gch文件到底是什么,为什么它要这样打我?(我知道…我知道…谷歌)。Serge+1@user690598:我认为*.gch是GCC的预编译头。
#include "Rectangle.h"
#include <cstddef>

using namespace shapes;

rectangle::rectangle(int h, int w)
{
    height = h;
    width = w;
    weight = NULL;
}

rectangle::rectangle()
{
    height = 0;
    width = 0;
    weight = NULL;
}

int rectangle::area ()
{
    return height * width;
}

void rectangle::setHeight(int h)
{
    height = h;
}

void rectangle::setWidth(int w)
{
    width = w;
}

void rectangle::setDimensions(int h, int w)
{
    height = h;
    width = w;
}
#include "Rectangle.h"
#include <iostream>

using namespace std;
using namespace shapes;

int main(int argc, char* argv[])
{
    rectangle tr(5,8);

    cout << tr.height << endl;
    cout << tr.width << endl;

    return 0;
}
#include "Rectangle.h"
#include <cstddef>

namespace shapes {

rectangle::rectangle(int h, int w)
{
    height = h;
    width = w;
    weight = NULL;
}

rectangle::rectangle()
{
    height = 0;
    width = 0;
    weight = NULL;
}

int rectangle::area ()
{
    return height * width;
}

void rectangle::setHeight(int h)
{
    height = h;
}

void rectangle::setWidth(int w)
{
    width = w;
}

void rectangle::setDimensions(int h, int w)
{
    height = h;
    width = w;
}

} // namespace shapes