Why can't I overload this constructor? [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am coding a class to help with models.
There is a snippet of code, this one (from the .cpp file):
Model GameModels::getModel(std::string& gameModelName){
return GameModelList.at(gameModelName);
}
Which throws this error:
Models.h:44:9: error: ‘Models::Model
Models::GameModels::getModel(std::string&)’ cannot be overloaded
Model getModel(std::string& gameModelName);
^ Models.h:40:9: error: with ‘Models::Model Models::GameModels::getModel(std::string&)’ Model
getModel(std::string& gameModelName);
on g++. Here is my Model definition:
struct Triangle{
VertexFormat x;
VertexFormat y;
VertexFormat z;
};
struct Model{
unsigned int vao;
std::vector<unsigned int> vbos;
std::vector<Triangle> geometry;
std::string shaderFilepath;
Model(){}
};

According to the error message (which has nothing to do with the posted code), you declare the same function twice, at lines 40 and 44 of Model.h.

Related

Passing data between variables inside a class in C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I don't fully understand nor did i find anything online about this topic but this code(cut down so it wont be 500 lines of code):
class Character{
public:
int MaxHP;
int currentHP;
currentHP = MaxHP;
int getHP()
{
return CurrentHP;
}
Character(int h){
maxHP=h
};
~Character(){};
};
int main()
{
Character warrior(300)
cout<<getHP();
return 0;
};
maxHP = 300
CurrentHP = -875000
now trying to acces the value of currentHP i get -87878....
My question is what is the problem?
If you wish to initialize a private variable it's better done in a constructor:
class Character{
private:
int MaxHP;
int currentHP;
public:
Character() : MaxHP(0), currentHP(MaxHP){}
}

Cannot call member function, tried to do it properly still fails [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I implemented a new class (ProtoType) in my header file. Which looks like this:
class ProtoType : public Test
{
public:
uint32_t test();
};
class RealProtoType : public Real
{
public:
uint32_t real();
};
Then in C++ file I made this
uint32_t ProtoType::test()
{
return 5;
}
uint32_t RealProtoType::real()
{
uint32_t holder = ProtoType::test();
}
Then I get this error when compiling
error: cannot call member function ‘uint32_t ProtoType::test()’
without object uint32_t ProtoType::test();
But I still fail, how can I resolve this?
Since ProtoType::test() is a non-static member function you need an object of type ProtoType to call the function upon:
uint32_t RealProtoType::real()
{
ProtoType foo;
uint32_t holder = foo.test();
return 42;
}

how can I bind a class member function with param as rvalue to boost::function? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I try to bind a class member function with param as rval to boost::function.
But it doesn't work.
my sample false code :
class Class1
{
int Foo1(int&& b)
{
return b;
}
void foo2()
{
boost::function<int(int&&)> fc(boost::bind(&Class1::Foo1, this, _1)
}
};
Use a lambda expression:
boost::function<int(int&&)> fc = [this](int&& x)
{
return Foo1(x);
};

confused about class structure in code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am reading a sample code that uses C++ and classes, I am new on C++ classes I can work with basics similar to this http://www.cplusplus.com/doc/tutorial/classes/, but I cant understand what the code below does mean or the color it is using visual studio c++
thanks
I am sorry if it is a fool question
It creates an object named some by instantiating the class some.
Then it calls the member function ToVector() on the object some and pass the result of the call to the function named function.
class is blue because it is a keyword of the C++ language.
The first some is green because it is the name of a class.
The second some is black because it is a variable.
And function and ToVector are red because the are functions.
Now this is ugly code because you "hide" the class some by reusing the same name for your variable. Also you do not need to put the word class here.
Here is a more complete and nicer version:
#include <vector>
class Some
{
public:
std::vector<int> ToVector()
{
return std::vector<int>(); //return an empty vector
}
};
int f(std::vector<int> v)
{
return 0;
}
int main(int, char**)
{
Some some; // Was "class some some"
return f(some.ToVector());
}

How to use variable type Struct inside class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to build a videogame with SDL for my end year proyect. However I'm somewhat lost about how and when to use classes.
T tried to include a variable type struct inside a class but I can't manage to do it, maybe Position should be a class instead of a struct? Here's my code:
struct Position{
int x,y;
};
class Object{
private:
Position pos;
Position speed;
int tipe;
public:
Objeto(int,int);
Objeto();
~Objeto(); // DESTROY
};
When I try to do this I get error: 'class Object' has no member named 'x' How can I include the struct in the object?
From your error, I think you are trying to use
Objeto.x
instead of
Objeto.pos.x