Link to home
Start Free TrialLog in
Avatar of gras5hopper
gras5hopper

asked on

Error 1 error C2664: 'Scroller::Scroller(float,float,std::vector<_Ty>,float,unsigned int,float)' :

I am creating a c++ game and have run into a snag, I am attempting to build a class which will allow me to declare different sprites and then make them scroll.

This is the full error:
Error      1      error C2664: 'Scroller::Scroller(float,float,std::vector<_Ty>,float,unsigned int,float)' : cannot convert parameter 3 from 'std::vector<_Ty>' to 'std::vector<_Ty>'      c:\users\krups\desktop\uni\sub_game\sub\csci2605cwk\mainscroller6.cpp      128

This is my header for the scrolling obstacles

[source]
//#ifndef SCROLL_SPRITE_H
//#define SCROLL_SPRITE_H
#include <SFML/Graphics.hpp>
using namespace sf;

class ObstacleSprite : public Sprite
{
public:
      ObstacleSprite();
      ObstacleSprite(const Image &Img, const Vector2f &Position=Vector2f(0, 0), const Vector2f &Scale=Vector2f(1, 1), float Rotation=0.f, const Color &Col=Color(255, 255, 255, 255),float scrollPos=0.0);
      void setScrollPos(float scrollPos);
      void setVertPos(float vertPos);
      float getVertPos(void);
      float getScrollPos(void);
protected:
      float scrollPosition;//small d - the sprite is positioned in the scroll between 0 and D
      float verticalPosition;//the y co-ord
      virtual void       Render (const RenderWindow &Window) const ;
};


//#endif

ObstacleSprite::ObstacleSprite():Sprite()
{
}

ObstacleSprite::ObstacleSprite(const Image &Img, const Vector2f &Position, const Vector2f &Scale, float Rotation, const Color &Col, float scrollPos):Sprite (Img, Position, Scale, Rotation, Col),scrollPosition(scrollPos)
{

}

void ObstacleSprite::setScrollPos(float scrollPos)
{
      scrollPosition=scrollPos;
}
void ObstacleSprite::setVertPos(float vertPos)
{
      verticalPosition=vertPos;
}

float ObstacleSprite::getVertPos(void)
{
      return verticalPosition;
}

void       ObstacleSprite::Render (const RenderWindow &Window) const
{
      Sprite::Render(Window);

}
float ObstacleSprite::getScrollPos()
{
      return scrollPosition;
}
[/source]

This is the .cpp and I have marked the line where the error occurs, any help would be greatly appreciated Thanks in advance.

[source]
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <sstream>
#include "ScrollSprite.h"
#include "Scroller.h"
#include "AnimatedSprite.h"
#include "Sub4.h"
#include "Boat2.h"
#include "SeaMine1.h"
#include "Rock2.h"
#include "ObstacleScroll1.h"
using namespace std;

int main()
{
      //initialisation - lots of this code could be put into functions for
      //neatness and separation of concerns. You need to declare objects here to draw them
      //but the set up for those objects could be done in a function or a class

      //window dimensions
      unsigned int width=600;
      unsigned int height=480;
      
      //we need a timer to update things - get a clock
      sf::Clock clock;
      static float timeAccumulator=0.0;

      //load resources for the scroller - images and sounds (if you have any)      
      sf::Image backGround;
      if(!backGround.LoadFromFile("images/background.jpg")) std::cout<<"File not found background.jpg"<<std::endl;
      
      //sf::Image aScroll;      
      //if(!aScroll.LoadFromFile("images/rock.png")) std::cout<<"File not found rock.png"<<std::endl;
      
            sf::Image bScroll;      
      if(!bScroll.LoadFromFile("images/boat.png")) std::cout<<"File not found rock.png"<<std::endl;
      //Load sub
            sf::Image subSprite;      
      if(!subSprite.LoadFromFile("images/subpng.png")) std::cout<<"File not found rock.png"<<std::endl;




      //resouces loaded

      sf::Sprite backGndSprite(backGround);
      backGndSprite.Scale(0.75f,0.8f);

// Sub
      //sf::Sprite playerSub(subSprite,Vector2f(Lead.GetX(),Lead.GetY()),Vector2f(2,2),0,Color(255,255,255,255));
      Submarine Lead;
      Lead.Init("images/subpng.png");
      //int health;
      Lead.setpower(100);

      Boat Boat1;
      Boat1.Init ("images/boat.png");

      Rock Rock1;
      Rock1.Init ("images/rock.png");

      SeaMine SeaMine1;
      SeaMine1.Init ("images/rocklol.png");

      //set up the scroller data
      //ScrollSprite scrollingSprite(Boat1);
      //scrollingSprite.setScrollPos(100);
      //scrollingSprite.setVertPos(300);
      ObstacleSprite ObSp1(bScroll);
      ObSp1.setScrollPos(100);
      ObSp1.setVertPos(300);

      std::vector<ObstacleSprite*> pSprite;
      //pSprite.push_back(&scrollingSprite);
      ////pSprite.push_back(&scrollingSprite1);
      pSprite.push_back(&ObSp1);

      //nb - scroller window width needs to match window size
>>error>>      Scroller scr(1400,width,pSprite,100);
      //


      // Create main window
    sf::RenderWindow App(sf::VideoMode(width, height), "Scroller Window");
      
    // Start the display loop
    while (App.IsOpened())
    {
            sf::Event Event;
        static int speed=1;//remembers its value through the loop
            //x = Lead.GetX();
            //y = Lead.GetY();
            //check inputs in the event queue
                         while (App.GetEvent(Event))
                        {
                              // Close window : exit
                              if (Event.Type == sf::Event::Closed)
                                    App.Close();
                              // Escape key : exit
                              if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                                    App.Close();
                              //Up arrow key :
                              if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up))
                              {
                                    //playerSub.Move(0,-4.0);      
                                    Lead.movesubup();

                              }
                              //Left arrow key :
                              if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left))
                              {
                                    Lead.movesubleft();
                                    cout<<"the power is "<<Lead.getpower();


                              }
                              //Right arrow key :
                              if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Right))
                              {
                                    //playerSub.Move(5.0,0);      
                                    Lead.movesubright();

                              }
                              //Down arrow key :
                              if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Down))
                              {
                                    //playerSub.Move(0,6.0);
                                    Lead.movesubdown();

                              }


                        }

                         

                         //update the things that need updating in the game loop
                        //update speed
                         float f=clock.GetElapsedTime();
                         clock.Reset();
                         timeAccumulator+=f;
                         //scr.update(f*speed);

                         

                         //draw the things you have to draw
                         App.Draw(backGndSprite);
                         App.Draw(scr);      
                               App.Draw(Lead.getSprite());
                              App.Draw(Boat1.getSprite());
                              App.Draw(Rock1.getSprite());
                              App.Draw(SeaMine1.getSprite());

                         

                         

                         //Monitoring the display function
                         std::stringstream str;
                         str<<"The display rate is ";                        
                         str<<1/f;
                         str<<"  the accumulated time is "<<timeAccumulator;
                         std::string s;
                         std::getline(str,s);
                         str.clear();                        
                         sf::String TextScore(s.c_str(),sf::Font::GetDefaultFont(),15);
                              TextScore.SetPosition(0,0);
                              TextScore.SetColor(sf::Color(255, 0, 0));
                              App.Draw(TextScore);
                              
                              
                  
        // Finally, display the rendered frame on screen
        App.Display();
    }

   
    return EXIT_SUCCESS;

}

[/source]

Avatar of Infinity08
Infinity08
Flag of Belgium image

>>       std::vector<ObstacleSprite*> pSprite;

pSprite is a vector of pointers to ObstacleSprite.

The error you get, indicates that the Scroller constructor takes a vector of a different type as third argument.

You didn't post the Scroller class though, so I can't check. But I'm sure you can check that for yourself ;)
Avatar of gras5hopper
gras5hopper

ASKER

Hi Infinity08, I don't follow, I am posting the scroller class if I can get a bit more info that would be excellent and helpful. Thanks for your previous reply too.
The scroller is in 2 files a scroller.h and a scroller.cpp

scroller.h
#ifndef SCROLLER_H
#define SCROLLER_H
#include <vector>
#include <SFML/Graphics.hpp>

#include "ScrollSprite.h"
using namespace sf;

class Scroller : public ScrollSprite
{
public:
      Scroller();
      Scroller(float D,float W,std::vector<ScrollSprite*> reps, float scrollV=0, unsigned int currentStartIndex=0,float left=0);
      void update(float dt);//moves lhs to correct position in the scroll pattern
      float getLHS();//returns the LHS value
      void setVelocity(float f);
      float getVelocity();
      
protected:
      float D;//the length in pixels of the scroll pattern
      float W;//the width in pixels of the scrolling window
      std::vector<ScrollSprite*> repeaters;//the repeating images that have to be drawn as we we move to the right - should point to scroll sprites
      float scrollVelocity;//how fast do we scoll;
      unsigned int currentStartIndex;
      float lhs;//the position in the scroll pattern of the window LHS 0<=lhs<D
      void Render (const RenderWindow &Window) const ;
      void prepareToRender();
private:
      void setHeights();
};



#endif

scroller.cpp
#include "Scroller.h"

Scroller::Scroller()
{
}
Scroller::Scroller(float D,float W,std::vector<ScrollSprite*> reps, float scrollV, unsigned int currentStartIndex,float left)
{
      this->D=D;
      this->W=W;
      this->repeaters=reps;
      this->scrollVelocity=scrollV;
      this->currentStartIndex=currentStartIndex;
      this->lhs=left;
      setHeights();
      
}
float Scroller::getLHS(void)
{
      return lhs;
}
void Scroller::update(float dt)
{
      lhs=lhs+dt*scrollVelocity;
      if(lhs>D)
      {
            lhs=lhs-D;
            currentStartIndex=0;
      }
      prepareToRender();
}

void Scroller::setVelocity(float v)
{
      scrollVelocity=v;
}

void       Scroller::Render (const RenderWindow &Window) const
{
            if(currentStartIndex<repeaters.size())
            //draw everything we can see starting from the current index
            //update index for the next run
            {
            //set the iterator to the current index
                  std::vector<ScrollSprite*>::const_iterator iter=repeaters.begin();
                  iter+=currentStartIndex;
                  
                  //draw the things between current index and the end which are in view
                  while(iter!=repeaters.end()&&((*iter)->getScrollPos()<=lhs+W))
                  {
                        
                        Window.Draw(**iter);
                        ++iter;
                  }
            }
       //need to check for wraparounds
      
      if(lhs+W>D)
            {
                  std::vector<ScrollSprite*>::const_iterator iterStart=repeaters.begin();

                  //draw everything that starts within W-(D-lhs) of the start
                  while(iterStart!=repeaters.end()&&((*iterStart)->getScrollPos()<W-(D-lhs)))
                  {                        
                        Window.Draw(**iterStart);
                        ++iterStart;
                  }            
            }
      
}

void Scroller::prepareToRender()
{
      if(currentStartIndex<repeaters.size())
            //setup everything we can see starting from the current index
            //update index for the next run
            {
            //set the iterator to the current index
                  std::vector<ScrollSprite*>::iterator iter=repeaters.begin();
                  iter+=currentStartIndex;

                  //move over anything which is too far left and update currentIndex for next run
                  while(iter!=repeaters.end()&&((*iter)->getScrollPos()<lhs)&&((*iter)->getScrollPos()+(*iter)->GetSize().x<lhs))
                  {
                        ++currentStartIndex;
                        ++iter;
                  }
                  //FYI currentIndex may be == repeaters.size()

                  //setup the things between current index and the end which are in view
                  while(iter!=repeaters.end()&&((*iter)->getScrollPos()<lhs+W))
                  {
                        //need to set some sprite data before drawing
                        float scrolPos=(*iter)->getScrollPos();
                        (*iter)->SetX(scrolPos-lhs);                                    
                        ++iter;
                  }
            }
       //need to check for wraparounds
      
      if(lhs+W>D)
            {
                  std::vector<ScrollSprite*>::iterator iterStart=repeaters.begin();

                  //draw everything that starts within W-(D-lhs) of the start
                  while(iterStart!=repeaters.end()&&((*iterStart)->getScrollPos()<W-(D-lhs)))
                  {
                        //need to set some sprite data before drawing
                        float scrolPos=(*iterStart)->getScrollPos();
                        (*iterStart)->SetX(D-lhs+scrolPos);                        
                        ++iterStart;

                  }            
            }


}

void Scroller::setHeights()
{

      std::vector<ScrollSprite*>::iterator iter;
      for(iter=repeaters.begin();iter<repeaters.end();iter++)
      {
            (*iter)->SetY((*iter)->getVertPos());
      }

}

float Scroller::getVelocity()
{
      return scrollVelocity;
};
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial