C++ Physical Architecture

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?
Error Messages:

1>------ Build started: Project: Physical Architecture (Development), Configuration: Debug Win32 ------
1> main.cpp
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall point::display(void)" (?display@point@@QAEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall point::setY(float)" (?setY@point@@QAEXM@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall point::setX(float)" (?setX@point@@QAEXM@Z) referenced in function _main
1>C:\Users\Me\Documents\Visual Studio 2010\C++\Projects\Physical Architecture (Development)\Debug\Physical Architecture (Development).exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
You must still compile all your .cpp files, but VS doesn't know about any file except main.cpp.

Make sure to add point.cpp in the solution explorer on the right.

Here's some useful reading about managing projects and files in VS.
https://msdn.microsoft.com/en-us/library/bb384842.aspx

Good luck!
Topic archived. No new replies allowed.