开发者

Header files. etc

I defined a simple class. The definition is in the header file and the implmentation is in the cpp file:

HEADER:

#ifndef POINT_H
#define POINT_H

class Point {
public:
    Point(开发者_如何转开发int x, int y);

    int getX();
    int getY();

private:
    int x, y;
};

#endif

CPP

#include Point.h

Point::Point(int x=2, int y=2) {
    this->x = x;
    this->y = y;
}

int Point::getX() {
    return x;
}

int Point::getY() {
    return y;
}

This is not compiling and I am not sure why. Also, when I want to instantiate a point somewhere else, say main.cpp, what do I #include at the top, just Point.h? If so, how does it know to look in Point.cpp?


#include Point.h 

You need quotes around Point.h.

Point::Point(int x=2, int y=2) 

The default arguments should be in the declaration in the header file, not in the definition in the .cpp file (it will compile as-is, but won't do what you want).

On a stylistic note, it's usually a bad idea to name local variables or arguments the same as member variables: doing so can easily lead to confusion. You can also initialize member variables using an initializer list:

Point::Point(int x_, int y_) : x(x_), y(y_) { }

As your code is currently written, x and y are both constructed, then you assign to them. Using an initializer list eliminates the assignment and just constructs them directly. This doesn't matter for fundamental types like int, but it does matter for class types.

This is not compiling and I am not sure why.

Usually the error message that the compiler issues is helpful. If nothing else, if it isn't helpful to you, it's helpful to those of us trying to help.

Also, when I want to instantiate a point somewhere else, say main.cpp, what do I #include at the top, just Point.h?

Yes.

How does it know to look in Point.cpp?

Each .cpp file that you write gets compiled into a separate object file. Once all the .cpp files are compiled into object files, the object files all get linked together into an excecutable (or a library). The linker figures out where everything is defined.


You are missing quotes in your include

#include "Point.h"

In the file where you want to instantiate a Point you must include Point.h


You should put the argument defaults in the header although without the error message I can't say if this is your problem. Also you should quote your include #include "Point.h"


You defined default values for x and y in the implementation. This can't work. Think a bit how this is supposed to work if the clients of your class only see the header and hence have no idea that they could possibly call Point without default parameters?

The trick is that the implementation is compiled into your application, and calls get resolved using the declaration only (the stuff in the header). For each call, the compiler creates a symbol name and marks the call (for instance, when you call Point::getX, the compiler will insert call which looks like call _getX@Point@). The linker, which comes after the compiler will search for this function. If it's implemented, it will be found and the caller will get "redirected" to the Point::getX() source code.

[Edit] Oh and you need to write #include "Point.h".


What is the error you're getting?

You just include the .h file:

#include "Point.h"

The linker resolves the references to code when it builds the executable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜