2017-10-12 99 views
1

我的問題是,我不知道如何與功能GetTexture和struct SDL_Rect工作*矩形(公共),內部主類中的私有部分類紋理試圖用類(SDL)

的成員,我宣佈:

Texture box("textures/box.png",100, 100, 20, 20); 

下面的語句放入主類MainLoop語句函數內部:

SDL_RenderCopy(renderer, box.GetTexture(), NULL, box->rect); 

行拋出錯誤:

\Main.cpp|28|error: '((Main*)this)->Main::box' does not have class type| 

\Main.cpp|28|error: invalid use of member function (did you forget the '()' ?)| 

\Main.cpp|28|error: base operand of '->' is not a pointer| 

這是Texture.h代碼:

#ifndef TEXTURE_H 
#define TEXTURE_H 
#include <string> 
#include <SDL.h> 
#include <SDL_image.h> 

using namespace std; 

class Texture 
{ 
    public: 
     Texture(string path, int x, int y, int w, int h); 
     virtual ~Texture(); 

     SDL_Texture GetTexture(); 

     SDL_Rect * rect; 
    protected: 
    private: 
     SDL_Texture * texture; 
}; 

#endif // TEXTURE_H 

而且Texture.cpp:

#include "Texture.h" 

using namespace std; 

Texture::Texture(string path, int x, int y, int w, int h) 
{ 
    texture = NULL; 
    texture = IMG_LoadTexture(renderer, path.c_str()); 
    rect.x = x; 
    rect.y = y; 
    rect.w = w; 
    rect.h = h; 
} 

Texture::~Texture() 
{ 
    //dtor 
} 

SDL_Texture Texture::GetTexture() 
{ 
    return texture; 
} 

我有點困惑,也許只是不明白目標規劃都還沒有。感謝您的幫助。

回答

0

- >操作符用於訪問指針的成員,而不是訪問非指針對象的指針成員。所以你應該使用:

box.rect 

而你應該使用它的方式,如果盒子是一個指針。

Texture* box = new Texture; 
box->rect 
+0

它幫助,指針錯誤解決。但仍存在問題: \ Main.cpp | 28 | error:'((Main *)this) - > Main :: box'沒有類型| - 它發生2次 – Papiero

+0

哦,對,因爲您使用了(),所以您將box聲明爲函數。你應該像這樣聲明它:紋理框;並使用{}將其初始化爲cpp文件或標題。 http://www.stroustrup.com/C++11FAQ.html#uniform-init – Murzinio