Recently I have been trying to organize my code a bit more efficiently as I've begun writing larger projects. Breaking up class declarations and definitions between headers and cpp files is one way I'm doing this (as recommended from many sources).
However, I have run into a problem. In the past, I've just put everything for a single class in a single header file. Then I put the header file in a Library/ folder which I can #include in my projects. However, if I separate the class code into both a header and a cpp, the cpp file cannot be found since it's not part of the project files.
For Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//Header file for a point class (point.h)
#pragma once
#ifndef _POINT_H
#define _POINT_H
class point {
private:
float x;
float y;
public:
void setX(float);
void setY(float);
void display();
};
#endif
And here is it's accompanying cpp file:
1 2 3 4 5 6 7 8 9 10
//CPP file for the point class (point.cpp)
#include <iostream>
#include "point.h"
void point::setX(float val) { x = val; }
void point::setY(float val) { y = val; }
void point::display() {
std::cout << "(" << x << ", " << y << ")";
}
When I attempt to #include the point class in my main.cpp, it gives me an error that basically means it isn't finding the cpp file, even though it is finding the class point declaration. Again, these two files are in a folder named "Library" with the intent of being #included in multiple programs as needed.
Main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include "../../../Libraries/point.h"
int main() {
point p;
p.setX(3);
p.setY(4);
p.display();
std::cin.get();
return 0;
}
So basic question, how do I "link" the cpp and header while keeping them in the Library folder so they can be used with multiple projects over time?