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<_T y>,float,u nsigned int,float)' : cannot convert parameter 3 from 'std::vector<_Ty>' to 'std::vector<_Ty>' c:\users\krups\desktop\uni \sub_game\ sub\csci26 05cwk\main scroller6. 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::ObstacleSp rite():Spr ite()
{
}
ObstacleSprite::ObstacleSp rite(const Image &Img, const Vector2f &Position, const Vector2f &Scale, float Rotation, const Color &Col, float scrollPos):Sprite (Img, Position, Scale, Rotation, Col),scrollPosition(scroll Pos)
{
}
void ObstacleSprite::setScrollP os(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::getScrollP os()
{
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.LoadFromFil e("images/ background .jpg")) std::cout<<"File not found background.jpg"<<std::endl ;
//sf::Image aScroll;
//if(!aScroll.LoadFromFile ("images/r ock.png")) std::cout<<"File not found rock.png"<<std::endl;
sf::Image bScroll;
if(!bScroll.LoadFromFile(" images/boa t.png")) std::cout<<"File not found rock.png"<<std::endl;
//Load sub
sf::Image subSprite;
if(!subSprite.LoadFromFile ("images/s ubpng.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,Vector 2f(Lead.Ge tX(),Lead. GetY()),Ve ctor2f(2,2 ),0,Color( 255,255,25 5,255));
Submarine Lead;
Lead.Init("images/subpng.p ng");
//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.setScrol lPos(100);
//scrollingSprite.setVertP os(300);
ObstacleSprite ObSp1(bScroll);
ObSp1.setScrollPos(100);
ObSp1.setVertPos(300);
std::vector<ObstacleSprite *> pSprite;
//pSprite.push_back(&scrol lingSprite );
////pSprite.push_back(&scr ollingSpri te1);
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.getSprit e());
//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::Fo nt::GetDef aultFont() ,15);
TextScore.SetPosition(0,0) ;
TextScore.SetColor(sf::Col or(255, 0, 0));
App.Draw(TextScore);
// Finally, display the rendered frame on screen
App.Display();
}
return EXIT_SUCCESS;
}
[/source]
This is the full error:
Error 1 error C2664: 'Scroller::Scroller(float,
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::ObstacleSp
{
}
ObstacleSprite::ObstacleSp
{
}
void ObstacleSprite::setScrollP
{
scrollPosition=scrollPos;
}
void ObstacleSprite::setVertPos
{
verticalPosition=vertPos;
}
float ObstacleSprite::getVertPos
{
return verticalPosition;
}
void ObstacleSprite::Render (const RenderWindow &Window) const
{
Sprite::Render(Window);
}
float ObstacleSprite::getScrollP
{
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.LoadFromFil
//sf::Image aScroll;
//if(!aScroll.LoadFromFile
sf::Image bScroll;
if(!bScroll.LoadFromFile("
//Load sub
sf::Image subSprite;
if(!subSprite.LoadFromFile
//resouces loaded
sf::Sprite backGndSprite(backGround);
backGndSprite.Scale(0.75f,
// Sub
//sf::Sprite playerSub(subSprite,Vector
Submarine Lead;
Lead.Init("images/subpng.p
//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.setScrol
//scrollingSprite.setVertP
ObstacleSprite ObSp1(bScroll);
ObSp1.setScrollPos(100);
ObSp1.setVertPos(300);
std::vector<ObstacleSprite
//pSprite.push_back(&scrol
////pSprite.push_back(&scr
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.getSprit
//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::Fo
TextScore.SetPosition(0,0)
TextScore.SetColor(sf::Col
App.Draw(TextScore);
// Finally, display the rendered frame on screen
App.Display();
}
return EXIT_SUCCESS;
}
[/source]
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=scrol lV;
this->currentStartIndex=cu rrentStart Index;
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(floa t v)
{
scrollVelocity=v;
}
void Scroller::Render (const RenderWindow &Window) const
{
if(currentStartIndex<repea ters.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_it erator iter=repeaters.begin();
iter+=currentStartIndex;
//draw the things between current index and the end which are in view
while(iter!=repeaters.end( )&&((*iter )->getScro llPos()<=l hs+W))
{
Window.Draw(**iter);
++iter;
}
}
//need to check for wraparounds
if(lhs+W>D)
{
std::vector<ScrollSprite*> ::const_it erator iterStart=repeaters.begin( );
//draw everything that starts within W-(D-lhs) of the start
while(iterStart!=repeaters .end()&&(( *iterStart )->getScro llPos()<W- (D-lhs)))
{
Window.Draw(**iterStart);
++iterStart;
}
}
}
void Scroller::prepareToRender( )
{
if(currentStartIndex<repea ters.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 )->getScro llPos()<lh s)&&((*ite r)->getScr ollPos()+( *iter)->Ge tSize().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 )->getScro llPos()<lh s+W))
{
//need to set some sprite data before drawing
float scrolPos=(*iter)->getScrol lPos();
(*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 )->getScro llPos()<W- (D-lhs)))
{
//need to set some sprite data before drawing
float scrolPos=(*iterStart)->get ScrollPos( );
(*iterStart)->SetX(D-lhs+s crolPos);
++iterStart;
}
}
}
void Scroller::setHeights()
{
std::vector<ScrollSprite*> ::iterator iter;
for(iter=repeaters.begin() ;iter<repe aters.end( );iter++)
{
(*iter)->SetY((*iter)->get VertPos()) ;
}
}
float Scroller::getVelocity()
{
return scrollVelocity;
};
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
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*>
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
{
this->D=D;
this->W=W;
this->repeaters=reps;
this->scrollVelocity=scrol
this->currentStartIndex=cu
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(floa
{
scrollVelocity=v;
}
void Scroller::Render (const RenderWindow &Window) const
{
if(currentStartIndex<repea
//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*>
iter+=currentStartIndex;
//draw the things between current index and the end which are in view
while(iter!=repeaters.end(
{
Window.Draw(**iter);
++iter;
}
}
//need to check for wraparounds
if(lhs+W>D)
{
std::vector<ScrollSprite*>
//draw everything that starts within W-(D-lhs) of the start
while(iterStart!=repeaters
{
Window.Draw(**iterStart);
++iterStart;
}
}
}
void Scroller::prepareToRender(
{
if(currentStartIndex<repea
//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*>
iter+=currentStartIndex;
//move over anything which is too far left and update currentIndex for next run
while(iter!=repeaters.end(
{
++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(
{
//need to set some sprite data before drawing
float scrolPos=(*iter)->getScrol
(*iter)->SetX(scrolPos-lhs
++iter;
}
}
//need to check for wraparounds
if(lhs+W>D)
{
std::vector<ScrollSprite*>
//draw everything that starts within W-(D-lhs) of the start
while(iterStart!=repeaters
{
//need to set some sprite data before drawing
float scrolPos=(*iterStart)->get
(*iterStart)->SetX(D-lhs+s
++iterStart;
}
}
}
void Scroller::setHeights()
{
std::vector<ScrollSprite*>
for(iter=repeaters.begin()
{
(*iter)->SetY((*iter)->get
}
}
float Scroller::getVelocity()
{
return scrollVelocity;
};
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ;)