This question already has answers here:
Why can in-class initializers only use = or {}? [duplicate]
(1 answer)
C++ Most vexing parse when a number literal is the argument? [duplicate]
(2 answers)
Closed 5 years ago.
I want to ask this question...
Why we can't use Camera c1(20); ???
This is making class.
But we find the error in Camera c1(20);
Please let me know...
#include <iostream>
class Camera{
private:
public:
Camera(){
}
Camera(int x){
}
};
class Phone{
private:
Camera c1(20);
public:
};
int main(){
}
To avoid parsing issue in general (See most vexing parse),
You may use Camera c1{20}; or Camera c1 = Camera(20);
I am trying to use vectors in sfml C++ and I am semi-failing because I am not able to create a vector consisting of 4 RectangleShapes :(
I am skipping some lines of the code which are not causing the problem
This is the code which is working but it is least optimized in my opinion
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <vector>
#include <iostream>
#include "mainCharacter.h" // it consists the mainCharacter class
class classBackground{
public:
sf::Texture texture;
sf::Sprite image;
sf::RectangleShape rectTopBorder,rectBotBorder,rectLeftBorder,rectRightBorder;
std::vector<sf::RectangleShape> rect;
classBackground(){
texture.loadFromFile("wagon.png");
image.setTexture(texture);
image.setPosition(300,250);
rectTopBorder.setSize(sf::Vector2f(230,5));
rectTopBorder.setPosition(image.getPosition());
rectTopBorder.move(0,60);
rectBotBorder.setSize(sf::Vector2f(230,5));
rectBotBorder.setPosition(image.getPosition());
rectBotBorder.move(0,225);
rectLeftBorder.setSize(sf::Vector2f(5,170));
rectLeftBorder.setPosition(image.getPosition());
rectLeftBorder.move(0,60);
rectRightBorder.setSize(sf::Vector2f(5,170));
rectRightBorder.setPosition(image.getPosition());
rectRightBorder.move(225,60);
rect.push_back(rectTopBorder);
rect.push_back(rectBotBorder);
rect.push_back(rectLeftBorder);
rect.push_back(rectRightBorder);
}
};
//in the main function
classMainCharacter mainCharacter; ///another class which has collision detection
classBackground background;
///in the loop
for(int i = 0; i<=3; i++){
mainCharacter.collision(background.rect[i]); ///colision is a function in the mainCharacter class (detects collision)
}
And this is the code which I want to work but my game is crushing :(
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <vector>
#include <iostream>
#include "mainCharacter.h" // it consists the mainCharacter class
class classBackground{
public:
sf::Texture texture;
sf::Sprite image;
std::vector<sf::RectangleShape> rect; // it doesn't work if I write rect(4) or rect = {rect1,rect2,rect3,rect3} or even rect(4)={rect1,....}
classBackground(){
texture.loadFromFile("wagon.png");
image.setTexture(texture);
image.setPosition(300,250);
rect[0].setSize(sf::Vector2f(230,5));
rect[0].setPosition(image.getPosition());
rect[0].move(0,60);
rect[1].setSize(sf::Vector2f(230,5));
rect[1].setPosition(image.getPosition());
rect[1].move(0,225);
rect[2].setSize(sf::Vector2f(5,170));
rect[2].setPosition(image.getPosition());
rect[2].move(0,60);
rect[3].setSize(sf::Vector2f(5,170));
rect[3].setPosition(image.getPosition());
rect[3].move(225,60);
}
};
//in the main function
classMainCharacter mainCharacter; ///another class which has collision detection
classBackground background;
///in the loop
for(int i = 0; i<=3; i++){
mainCharacter.collision(background.rect[i]); ///colision is a function in the mainCharacter class (detects collision)
}
what am I doing wrong? :(
I tried to defined the rects before vector and then inserting into the vector but it also doesn't work (in such way vector<...> rect = {......}).
Well even if I would not expect to much difference between the two versions, an answer is worth telling anyway as your problem has neither to do with performance nor with sfml at all but is more general.
What you try is to instantiate a class member at its declaration, what is not possible. But what you can do is either one of the following methods:
1) Use the default member initializer with the C++ 11 unified initialization syntax like so (wich may only be syntactic sugar for (2) prior to this: http://en.cppreference.com/w/cpp/language/data_members#Member_initialization):
std::vector<sf::RectangleShape> rect{4}
2) Use the initializer list in the constructor:
classBackground() : rect(4) {...}
3) Or call the resize method in the constructor:
classBackground(){
rect.resize(4);
...
}
The last one might be the worst as it first calls the standard constructor of vector<...> and changes it afterwards.
In the first version, you were successfully creating a vector of 4 elements by using push_back.
In the second version, you never allocate space for your 4 elements, so when you try and assign values to the elements of the vector, that's your crash.
If you really don't want to use push_back you can instead use resize to set the size of the vector.
Here's a simplified version of what you're trying to do
#include <vector>
class classBackground {
std::vector<int> rect;
public:
classBackground() {
rect.resize(4);
rect[0] = 1;
rect[1] = 2;
rect[2] = 3;
rect[3] = 4;
}
};
int main() {
classBackground cb;
// there, it worked.
}
Demo: http://ideone.com/YYq0oA
Alternatively if you have C++11 and your vector is always going to be 4 elements large, which in this case it seems like it might be, you should consider using std::array instead.
#include <array>
class classBackground {
std::array<int, 4> rect;
public:
classBackground() {
rect[0] = 1;
rect[1] = 2;
rect[2] = 3;
rect[3] = 4;
}
};
int main() {
classBackground cb;
// there, it worked.
}
Array works like vector in most ways, you just can't change the size, but it can potentially be more efficient.
This question already has answers here:
constructors basic (reference to distance is ambiguous)
(3 answers)
Closed 7 years ago.
While compiling the following code i am getting the error as
reference to 'distance' is ambiguous
#include<iostream>
using namespace std;
class distance
{
int feet,inches;
distance():feet(0),inches(0)
{
}
distance(int f,int i):feet(f),inches(i)
{
}
void show()
{
cout<<"feet "<<feet;
cout<<endl<<"inches "<<inches;
}
distance operator + (distance) ;
};
distance distance::operator + (distance d)
{
int f,i;
f=feet+d.feet;
i=inches+d.inches;
return distance(f,i);
}
int main()
{
distance d1;
distance d2(2,3),d3(7,5);;
d1=d2+d3;
d1.show();
}
can anyone help me with the error.
And provide me the solution and as to why i am getting this error.
And this is why using namespace std; should not be used. Your class distance is clashing with the standard function std::distance. Get rid of the using namespace std; and if you are going to be using a standard component use std::name_of_thing every time you use it or you can use using std::name_of_thing.
Your class name is clashing with another symbol from the namespace, changing your class name to something else like Distance would be one possible solution.
This question already has answers here:
Can I call a constructor from another constructor (do constructor chaining) in C++?
(15 answers)
Closed 7 years ago.
class draw
{
draw(circle i)
{
// draw a circle;
}
draw(circle i, circle j)
{
draw(i);
draw(j);
}
}
Can I call another overloaded constructor of the same class in C++ even if using template?
No, in C++ you cannot have one constructor directly call another (well, at least not more than once). However, you can have each constructor call a third method that performs actual work.
class draw
{
draw(circle i)
{
do_draw(i);
}
draw(circle i, circle j)
{
do_draw(i);
do_draw(j);
}
void do_draw(circle c)
{
// draw a circle;
}
}
This question already has answers here:
How do I pass a reference to a two-dimensional array to a function?
(5 answers)
Reference to a Two-Dimesional Array
(2 answers)
Closed 8 years ago.
I have a problem. I dont know how to pass 2D array of pointers to fuction by reference.
class SomeClass
{
//body of class
}
void somefunction(SomeClass ***array)
{
//body of function
}
int main()
{
SomeClass * array[10][10]
someFunction(array?????)
}
Anyone know how to pass this array by reference??
Literally
void somefunction(SomeClass *(&array)[10][10])
{
}
int main()
{
SomeClass *array[10][10];
someFunction(array);
}