错误 c++ visual studio c2227 '->Init' 的左侧必须指向 class/struct/union/generic 类型
error c++ visual studio c2227 Left of '->Init' must point to class/struct/union/generic type
我制作了一个简单的游戏 window,可以加载 2 个精灵,但我改变了加载它的方式,因此最好为每个精灵创建另一个变量。
我收到以下错误:
c2227 left of '->Init' must point to class/struct/union/generic type
c2227 left of '->Init' must point to class/struct/union/generic type
这是我的代码:
MainGame.h
#pragma once
#include <SDL/SDL.h>
#include <GL/glew.h>
#include <vector>
#include "GLTexture.h"
#include "GLSLProgram.h"
#include "Sprite.h"
enum class GameState {PLAY, EXIT};
class MainGame
{
public:
MainGame();
~MainGame();
void run();
void drawGame();
private:
void initSystems();
void initShaders();
void gameLoop();
void processInput();
SDL_Window* _window;
int _ScreenWidth;
int _ScreenHeight;
GameState _gameState;
std::vector <Sprite*> _sprites;
GLSLProgram _colorProgram;
float _time;
};
MainGame.ccp
#include "MainGame.h"
#include <iostream>
#include <string>
#include "Errors.h"
MainGame::MainGame() : _ScreenWidth(1024),
_ScreenHeight(768),
_window(nullptr),
_gameState(GameState::PLAY)
{
}
//Destructer
MainGame::~MainGame()
{
}
void MainGame::run() {
initSystems();
_sprites.push_back(new Sprite());
_sprites.back->Init(0.0f, -1.0f, 1.0f, 1.0f, "Textures/jimmyJump_pack/PNG/CharacterRight_Standing.png");
_sprites.push_back(new Sprite());
_sprites.back->Init(-1.0f, -1.0f, 1.0f, 1.0f, "Textures/jimmyJump_pack/PNG/CharacterRight_Standing.png");
//_playerTexture = ImageLoader::loadPNG("Textures/jimmyJump_pack/PNG/CharacterRight_Standing.png");
gameLoop();
}
void MainGame::initSystems() {
//Init SDL
SDL_Init(SDL_INIT_EVERYTHING);
//Set up Window
_window = SDL_CreateWindow("Iskallium Engine >> Game Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _ScreenWidth, _ScreenHeight, SDL_WINDOW_OPENGL);
if (_window == nullptr) {
fatalError("Iskallium Engine could not be opened");
}
//Set up OpenGL
SDL_GLContext glContext = SDL_GL_CreateContext(_window);
if (glContext == nullptr) {
fatalError("Iskallium Engine could not start Model-Loader");
}
//Set up glew
glewExperimental = true;
GLenum error = glewInit();
if (error != GLEW_OK) {
fatalError("Could not initialize glew!");
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glClearColor(0.0f, 0.0f ,1.0f, 1.0f);
initShaders();
}
void MainGame::initShaders() {
_colorProgram.compileShaders("Shaders/colorShading.vert", "Shaders/colorShading.frag");
_colorProgram.addAttribute("vertexPosition");
_colorProgram.addAttribute("vertexColor");
_colorProgram.addAttribute("vertexUV");
_colorProgram.linkSchaders();
}
void MainGame::gameLoop() {
while (_gameState != GameState::EXIT) {
processInput();
drawGame();
}
}
void MainGame::processInput() {
SDL_Event evnt;
while (SDL_PollEvent(&evnt)) {
switch (evnt.type) {
case SDL_QUIT:
_gameState = GameState::EXIT;
break;
case SDL_MOUSEMOTION:
//std::cout << evnt.motion.x << " " << evnt.motion.y << std::endl;
break;
}
}
}
void MainGame::drawGame() {
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_colorProgram.use();
glActiveTexture(GL_TEXTURE0);
GLint textureLocation = _colorProgram.getUniformLocation("mySampler");
glUniform1i(textureLocation, 0);
//Draw sprite
for (int i = 0; i < _sprites.size(); i++) {
_sprites[i]->draw();
}
glBindTexture(GL_TEXTURE_2D, 0);
_colorProgram.unuse();
//Swapping buffer Window
SDL_GL_SwapWindow(_window);
}
如果您需要更多代码或其他 类 只需询问 :)
这应该会在屏幕上加载 2 个 Sprites,正如您从我的代码中看到的那样,我对此非常陌生,所以有些代码看起来像菜鸟代码:(,请不要纠正我的错误,因为我我知道一些事情我没有 100% 正确,但其他代码都不会破坏我的 ->Init Sprite 行所以请帮助我,而不是帮助我如何让我的代码看起来不像新手代码:D
已经非常感谢你的所有回复,希望我们能解决它。
试试
_sprites.back()->Init(0.0f, -1.0f, 1.0f, 1.0f, "Textures/jimmyJump_pack/PNG/CharacterRight_Standing.png");
需要->back
后的括号,这是一种方法。
我制作了一个简单的游戏 window,可以加载 2 个精灵,但我改变了加载它的方式,因此最好为每个精灵创建另一个变量。 我收到以下错误:
c2227 left of '->Init' must point to class/struct/union/generic type
c2227 left of '->Init' must point to class/struct/union/generic type
这是我的代码:
MainGame.h
#pragma once
#include <SDL/SDL.h>
#include <GL/glew.h>
#include <vector>
#include "GLTexture.h"
#include "GLSLProgram.h"
#include "Sprite.h"
enum class GameState {PLAY, EXIT};
class MainGame
{
public:
MainGame();
~MainGame();
void run();
void drawGame();
private:
void initSystems();
void initShaders();
void gameLoop();
void processInput();
SDL_Window* _window;
int _ScreenWidth;
int _ScreenHeight;
GameState _gameState;
std::vector <Sprite*> _sprites;
GLSLProgram _colorProgram;
float _time;
};
MainGame.ccp
#include "MainGame.h"
#include <iostream>
#include <string>
#include "Errors.h"
MainGame::MainGame() : _ScreenWidth(1024),
_ScreenHeight(768),
_window(nullptr),
_gameState(GameState::PLAY)
{
}
//Destructer
MainGame::~MainGame()
{
}
void MainGame::run() {
initSystems();
_sprites.push_back(new Sprite());
_sprites.back->Init(0.0f, -1.0f, 1.0f, 1.0f, "Textures/jimmyJump_pack/PNG/CharacterRight_Standing.png");
_sprites.push_back(new Sprite());
_sprites.back->Init(-1.0f, -1.0f, 1.0f, 1.0f, "Textures/jimmyJump_pack/PNG/CharacterRight_Standing.png");
//_playerTexture = ImageLoader::loadPNG("Textures/jimmyJump_pack/PNG/CharacterRight_Standing.png");
gameLoop();
}
void MainGame::initSystems() {
//Init SDL
SDL_Init(SDL_INIT_EVERYTHING);
//Set up Window
_window = SDL_CreateWindow("Iskallium Engine >> Game Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _ScreenWidth, _ScreenHeight, SDL_WINDOW_OPENGL);
if (_window == nullptr) {
fatalError("Iskallium Engine could not be opened");
}
//Set up OpenGL
SDL_GLContext glContext = SDL_GL_CreateContext(_window);
if (glContext == nullptr) {
fatalError("Iskallium Engine could not start Model-Loader");
}
//Set up glew
glewExperimental = true;
GLenum error = glewInit();
if (error != GLEW_OK) {
fatalError("Could not initialize glew!");
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glClearColor(0.0f, 0.0f ,1.0f, 1.0f);
initShaders();
}
void MainGame::initShaders() {
_colorProgram.compileShaders("Shaders/colorShading.vert", "Shaders/colorShading.frag");
_colorProgram.addAttribute("vertexPosition");
_colorProgram.addAttribute("vertexColor");
_colorProgram.addAttribute("vertexUV");
_colorProgram.linkSchaders();
}
void MainGame::gameLoop() {
while (_gameState != GameState::EXIT) {
processInput();
drawGame();
}
}
void MainGame::processInput() {
SDL_Event evnt;
while (SDL_PollEvent(&evnt)) {
switch (evnt.type) {
case SDL_QUIT:
_gameState = GameState::EXIT;
break;
case SDL_MOUSEMOTION:
//std::cout << evnt.motion.x << " " << evnt.motion.y << std::endl;
break;
}
}
}
void MainGame::drawGame() {
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_colorProgram.use();
glActiveTexture(GL_TEXTURE0);
GLint textureLocation = _colorProgram.getUniformLocation("mySampler");
glUniform1i(textureLocation, 0);
//Draw sprite
for (int i = 0; i < _sprites.size(); i++) {
_sprites[i]->draw();
}
glBindTexture(GL_TEXTURE_2D, 0);
_colorProgram.unuse();
//Swapping buffer Window
SDL_GL_SwapWindow(_window);
}
如果您需要更多代码或其他 类 只需询问 :)
这应该会在屏幕上加载 2 个 Sprites,正如您从我的代码中看到的那样,我对此非常陌生,所以有些代码看起来像菜鸟代码:(,请不要纠正我的错误,因为我我知道一些事情我没有 100% 正确,但其他代码都不会破坏我的 ->Init Sprite 行所以请帮助我,而不是帮助我如何让我的代码看起来不像新手代码:D
已经非常感谢你的所有回复,希望我们能解决它。
试试
_sprites.back()->Init(0.0f, -1.0f, 1.0f, 1.0f, "Textures/jimmyJump_pack/PNG/CharacterRight_Standing.png");
需要->back
后的括号,这是一种方法。