Cannot convert argument 1 from 'Object *' to 'const int

I want to create a simple program that demonstrates the usage of this. I wrote a function that prints the x and y membervariables. I defined the function on top of the file. I acutally don't even know why that is needed but ok. So far so good. Visual Studio finds zero issues, no warnings and no erros. However when I compile this code I get the following error message:

1>D:\Dev\Visual Studio Projekte\Projektmappe\FirstProject\src\This.cpp(3,30): error C2143: syntax error: missing ',' before '*'
1>D:\Dev\Visual Studio Projekte\Projektmappe\FirstProject\src\This.cpp(11,1): error C2664: 'void PrintObject(const int)': cannot convert argument 1 from 'Object *' to 'const int'

Now at some point I also got the error that no default constructor is defined. However the compiler does provide one right? I then simply retyped the code and the error disappeared. Just something I wanted to mention that also seemed stupid. So why is the compiler thinking that "this" is referring to a "const int" when it is in fact referring to my object?

#include <iostream> void PrintObject(const Object* o); class Object { public: int x, y; Object(int x, int y) : x(x), y(y) { PrintObject(this); } int GetX() const { return this->x; } int GetXy() const { return x; } }; void PrintObject(const Object* o) { std::cout << o->x << ", " << o->y << std::endl; } int main() { std::cin.get(); return 0; } 
1

3 Answers

void PrintObject(const Object* o); 

You haven't declared Object yet, so the compiler cannot know what it is. You must first declare the class before delaring this function. Technically, they can both be declared at the same time, but that isn't a very popular style.

I also got the error that no default constructor is defined. However the compiler does provide one right?

It does not provide one. Default constructor is only provided for classes with no user declared constructors (and no non-default constructible sub objects).

It's simple enough, you are using Object before you are declaring it. So the compiler doesn't know what Object is at the time it reads the declaration of PrintObject.

Two possible solutions.

1) Move the declaration of PrintObject after the definition of Object.

class Object { ... }; void PrintObject(const Object* o); 

2) Use a forward declaration to tell the compiler that Object is the name of a class.

class Object; // forward declaration void PrintObject(const Object* o); class Object { ... }; 
2

PrintObject could be defined as a member function. Would make the syntax much nicer.

#include <iostream> class Object { public: Object(int x, int y) : x(x), y(y) { PrintObject(); } int GetX() const { return this->x; } int GetXy() const { return x; } void PrintObject() const { std::cout << x << ", " << y << std::endl; } private: int x, y; }; int main() { Object o{5,6}; return 0; } 

Also you might want to consider, overwriting the output stream operator, if you want to define custom print for your type.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like