error c++ visual studio c2227 Left of '->Init&

2019-06-10 08:48发布

问题:

I have made a simple game window that loads 2 sprites but i changed my way of loading it so it would be better then creating for every Sprite a other variable. I'm getting the following error:

c2227  left of '->Init' must point to class/struct/union/generic type
c2227  left of '->Init' must point to class/struct/union/generic type

And this is my code:

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);
}

if you need any more code or other classes just ask :)

This should load 2 Sprites of on the screen as you can see from my code, im fearly new to this so some of the code will look like noob code :(, please don't correct me on that because i know somethings i do not 100% correctly but nothing of the other code should corrupt my ->Init Sprite line so please help me on that and not on how to make my code don't look noob code :D

Already a HUGE thank you for all the replies i will get, hope we can solve it.

回答1:

Try with

_sprites.back()->Init(0.0f, -1.0f, 1.0f, 1.0f, "Textures/jimmyJump_pack/PNG/CharacterRight_Standing.png");

Parenthesis after ->back are needed, it's a method.