Try implementing code like this (the conventional way):
main.cpp :
#include "point.hpp"
#include <iostream>
int main() {
Point A1; // Point A1(1, 2, 3);
std::cout << "The function prints out: \n";
A1.print();
std::cout << "The result of norm is: " << A1.norm() << std::endl;
A1.negate();
std::cout << "The result of negate: \n";
A1.print();
return 0;
}
point.hpp : (just has prototype declarations)
#ifndef POINT_HEADER_INCLUDE_GUARD
#define POINT_HEADER_INCLUDE_GUARD
#include <cmath>
#include <iostream>
class Point {
int x, y, z;
public:
Point();
Point(int, int, int);
void print();
float norm();
void negate();
void set();
};
#endif
point.cpp : (has definitions of the things declared in point.hpp)
#include "point.hpp"
Point::Point() { set(); }
Point::Point(int a, int b, int c) {
x = a;
y = b;
z = c;
}
void Point::print() {
std::cout << "The x value is : " << x << std::endl;
std::cout << "The y value is : " << y << std::endl;
std::cout << "The z value is : " << z << std::endl;
}
float Point::norm() {
float t = x * x + y * y + z * z;
float s = std::sqrt(t);
return s;
}
void Point::negate() {
x = x * -1;
y = y * -1;
z = z * -1;
}
void Point::set() {
std::cout << "Please provide X,Y,Z values: ";
std::cin >> x >> y >> z;
std::cout << "The x value is : " << x << std::endl;
std::cout << "The y value is : " << y << std::endl;
std::cout << "The z value is : " << z << std::endl;
}
Compile using some command like g++ -o main main.cpp header.cpp -I ./
. It may change depending on your compiler but in any case you need to compile and link both the files.
EDIT: For C++ files, prefer naming the file the same as the (main) class it contains. Also prefer keeping the file names as all-lowercase, even though class names contain capital letters. It is OK to use commonly known abbreviations, and/or omit the name of the containing directory if that would cause unnecessary repetition (e.g., as a common prefix to every file name in the directory) and the remaining part of the name is unique enough.
BTW the mess in your original question was not at all reproducible. We all were getting errors due to using namespace std;
!
These threads are linked with this question:
- How to create my own header file in c++?
- What Should go in my Header File in C++?