Defining header files in C++ - c++

To start off with, I'll mention I come mainly from a Java background. I do have exposure with C and understand most concepts behind C++. I'm trying to help myself learn more about the language and can't seem to figure out headers. I understand why to use them in addition to cpp files and all of that. My problem is trying to actually manage working with them. For example, defining a Vector3 header with private float variables and then overload operating. My problem comes in when I attempt to define the constructor and methods in the cpp file. I can't seem to figure out how to get access to the private variables without specifically defining the functions and the constructor in the header, which more or less leads me to believe I don't need both a header and cpp file in this instance.
Here's how I've defined the header file currently (which works, but isn't undefined as it should be):
#pragma once
#ifndef __Vector_3_H__
#define __Vector_3_H__
namespace VectorMath {
class Vector3 {
public:
Vector3(float x, float y, float z) {
this->x = x;
this->y = y;
this->z = z;
}
Vector3 operator+(Vector3 vector) {
return Vector3(x + vector.x, y + vector.y, z + vector.z);
}
Vector3 operator-(Vector3 vector) {
return Vector3(x - vector.x, y - vector.y, z - vector.z);
}
Vector3 operator*(Vector3 vector) {
return Vector3(x * vector.x, y * vector.y, z * vector.z);
}
Vector3 operator/(Vector3 vector) {
return Vector3(x / vector.x, y / vector.y, z / vector.z);
}
float getX() {
return x;
}
float getY() {
return y;
}
float getZ() {
return z;
}
private:
float x;
float y;
float z;
};
}
#endif

It needs to look more like this instead:
Vector_3.h:
#ifndef Vector_3_H
#define Vector_3_H
#pragma once
namespace VectorMath {
class Vector3 {
public:
Vector3(float x, float y, float z);
Vector3 operator+(Vector3 vector);
Vector3 operator-(Vector3 vector);
Vector3 operator*(Vector3 vector);
Vector3 operator/(Vector3 vector);
float getX();
float getY();
float getZ();
private:
float x;
float y;
float z;
};
}
#endif
Vector_3.cpp:
#include "Vector_3.h"
namespace VectorMath {
Vector3::Vector3(float x, float y, float z) {
this->x = x;
this->y = y;
this->z = z;
}
Vector3 Vector3::operator+(Vector3 vector) {
return Vector3(x + vector.x, y + vector.y, z + vector.z);
}
Vector3 Vector3::operator-(Vector3 vector) {
return Vector3(x - vector.x, y - vector.y, z - vector.z);
}
Vector3 Vector3::operator*(Vector3 vector) {
return Vector3(x * vector.x, y * vector.y, z * vector.z);
}
Vector3 Vector3::operator/(Vector3 vector) {
return Vector3(x / vector.x, y / vector.y, z / vector.z);
}
float Vector3::getX() {
return x;
}
float Vector3::getY() {
return y;
}
float Vector3::getZ() {
return z;
}
}

If you want to use a cpp file for your constructor you should write
// File Vector3.cpp
#include "Vector3.h"
namespace VectorMath {
Vector3::Vector3 (float x, float y, float z)
{
this->x=x;
//...
}
The addition should be implemented as follows if you keep it in the same namespace
Vector3 Vector3::operator+(const Vector3& v)
{
return Vector3 (x+v.x,y+v.y,z+v.z);
}
}

If you want to move the implementations of member functions away from the header file, you still need to declare them in the definition of the class. For example:
// Vector1.h
#pragma once
#ifndef VectorMath_Vector1_H
#define VectorMath_Vector1_H
namespace VectorMath {
class Vector1 {
public: // Methods:
// This is a definition for a default constructor:
Vector1() noexcept : m_x(0) {}
// This is a declaration for another constructor:
Vector1(float x) noexcept;
// This is a declaration of a member function:
Vector1 operator+(Vector1 const & rhs) const noexcept;
private: // Fields:
float m_x;
}; // class Vector1
} // namespace VectorMath {
#endif // VectorMath_Vector1_H
// Vector1.cpp
#include "Vector1.h"
namespace VectorMath {
// Definition of the other constructor:
Vector1::Vector1(float x) noexcept
: m_x(x)
{}
// Definition of the binary + operator:
Vector1 Vector1::operator+(Vector1 const & rhs) const noexcept
{ return m_x + rhs.m_x; }
} // namespace VectorMath {

Related

How to use classes inside headers in c++

So i'm very new to c++ and i'm trying to test out the simple features it has. I currently have a problem that there is a 'class' type redefinition and I can't figure out why. I have used #pregama once in the header file, but still. I have even tried #ifndef and #define, but they did not work either. I'm using Visual studio 2019 community if that has anything to do with this.
Main.cpp
#include <iostream>
#include "Vectors.h"
using namespace std;
int main() {
double distance = Vector2::Zero().Distance(Vector2(1, 1));
cout << distance;
return 0;
}
Vector2.cpp
#include "Vectors.h"
#include <cmath>
class Vector2 {
public:
double x;
double y;
Vector2(double x, double y) {
this->x = x;
this->y = y;
}
static Vector2 Zero() {
return Vector2(0, 0);
}
static Vector2 One() {
return Vector2(1, 1);
}
double Distance(Vector2 other) {
Vector2 relPos = this->operator-(other);
relPos.x = abs(relPos.x);
relPos.y = abs(relPos.y);
return sqrt(pow(relPos.x, 2) + pow(relPos.y, 2));
}
Vector2 operator + (Vector2 vec) {
return Vector2(x + vec.x, y + vec.y);
}
Vector2 operator - (Vector2 vec) {
return Vector2(x - vec.x, y - vec.y);
}
Vector2 operator * (Vector2 vec) {
return Vector2(x * vec.x, y * vec.y);
}
Vector2 operator / (Vector2 vec) {
return Vector2(x / vec.x, y / vec.y);
}
};
Vectors.h
#pragma once
class Vector2 {
public:
Vector2(double x, double y);
static Vector2 Zero();
static Vector2 One();
double Distance(Vector2 other);
Vector2 operator + (Vector2 vec);
Vector2 operator - (Vector2 vec);
Vector2 operator * (Vector2 vec);
Vector2 operator / (Vector2 vec);
};
You're defining the class once in the header and once in the C++ file. You don't want to do class Vector2 { in the C++ file. Your functions should look like this
Vector2 Vector2::operator+(Vector2 vec) {
return Vector2(x + vec.x, y + vec.y);
}
And should be defined at top-level scope in the C++ file.
First of all, I agree with Silvio Mayolo, You're defining the class once in the header and once in the C++ file. You don't use the keyword class in your cpp file.
And then you need to distinguish between class Vector2 and Vector2(double x, double y); As far as I'm concerned you should try to use Vector2::.
Here is my code:
Vectors.h:
#pragma once
class Vector2 {
public:
double x;
double y;
Vector2(double x, double y);
static Vector2 Zero();
static Vector2 One();
double Distance(Vector2 other);
Vector2 operator + (Vector2 vec);
Vector2 operator - (Vector2 vec);
Vector2 operator * (Vector2 vec);
Vector2 operator / (Vector2 vec);
};
Vector2.cpp:
#include "Vectors.h"
#include <cmath>
double x;
double y;
Vector2::Vector2(double x, double y)
{
this->x = x;
this->y = y;
}
Vector2 Vector2::Zero() {
return Vector2(0, 0);
}
Vector2 Vector2::One() {
return Vector2(1, 1);
}
double Vector2::Distance(Vector2 other) {
Vector2 relPos = this->operator-(other);
relPos.x = abs(relPos.x);
relPos.y = abs(relPos.y);
return sqrt(pow(relPos.x, 2) + pow(relPos.y, 2));
}
Vector2 Vector2::operator + (Vector2 vec) {
return Vector2(x + vec.x, y + vec.y);
}
Vector2 Vector2::operator - (Vector2 vec) {
return Vector2(x - vec.x, y - vec.y);
}
Vector2 Vector2::operator * (Vector2 vec) {
return Vector2(x * vec.x, y * vec.y);
}
Vector2 Vector2::operator / (Vector2 vec) {
return Vector2(x / vec.x, y / vec.y);
}
Main.cpp:
#include <iostream>
#include "Vectors.h"
using namespace std;
int main() {
double distance = Vector2::Zero().Distance(Vector2(1, 1));
cout << distance;
return 0;
}

C++ issue with standard library

I'am making this post because i have for the first time of my life an incomprehensible error from visual studio community 2017 :
I simply can't use others members of std then "nullptr_t" in all my classes except in the main.cpp ....
The auto-completion of visual only suggest me "nullptr_t" when I write "std::" except in the main.cpp. It's incomprehensible.
main.cpp code:
#include <SFML/Graphics.hpp>
int main()
{
return 0;
}
Point class code :
#pragma once
class Point
{
private:
float _x;
float _y;
public:
Point();
Point(float x, float y);
Point(const Point& p);
~Point();
float getx() { return _x; }
float gety() { return _y; }
void setx(float x) { _x = x; }
void sety(float y) { _y = y; }
};
/************************************************************************/
#include "pch.h"
#include "Point.h"
Point::Point()
{
_x = 0.0;
_y = 0.0;
}
Point::Point(float x, float y)
{
_x = x;
_y = y;
}
Point::Point(const Point& p)
{
_x = p._x;
_y = p._y;
}
Point::~Point()
{
}
Hexagon class :
#pragma once
class Hexagon
{
private:
Point _center;
float _diameter;
public:
Hexagon();
Hexagon(Point center, float diameter);
~Hexagon();
};
/***********************************************************************/
#include "pch.h"
#include "Hexagon.h"
#include "Point.h"
Hexagon::Hexagon() : _center()
{
_diameter = 10;
}
Hexagon::Hexagon(Point center, float diameter) : _center(center)
{
_diameter = diameter;
}
Hexagon::~Hexagon()
{
}
You just need to include the iostream library or add this line of code where you need it to the includes.
#include<iostream>

Class has more than one constructor

Currently, im working on an CVector class, everything was working fine until i wanted to use vectors in other classes like CVector v; and use the v later.
well, the problem lies there - i would use
struct Vector3_t {
float x, y , z;
};
but i want to use operators for those vectors, so i made a class:
class CVector
{
public:
//missing usage: CVector vec; // for later usage in example.
CVector() // usage: CVector v();
{
this->x = 0, this->y = 0, this->z = 0;
}
CVector(const float x = 0, const float y = 0) { // usage: CVector v(1,2); // but z is always 0
this->x = x, this->y = y,this->z = 0;
}
CVector(const float x = 0, const float y = 0, const float z = 0) { // usage: CVector v(1,2,3);
this->x = x, this->y = y, this->z = z;
}
CVector & CVector::operator += (const CVector & v) {
this->x += v.x; this->y += v.y; this->z += v.z; return *this;
}
const CVector CVector::operator + (const CVector& v) const {
CVector r(*this); return r += v;
}
float x, y, z;
~CVector() {};
protected:
private:
};
in action:
int main() {
CVector vec;
return 0;
}
output errors:
Severity Code Description Project File Line Suppression State
Error (active) class "CVector" has more than one default constructor mebad c:\Users\lifesucks\Documents\Visual Studio 2015\Projects\mebad\mebad\main.cpp 43
*Severity Code Description Project File Line Suppression State
Error C2668 'CVector::CVector': ambiguous call to overloaded function mebad c:\users\lifesucks\documents\visual studio 2015\projects\mebad\mebad\main.cpp 43
*
basically i just have no idea how to declare class for this kind of use when having multiple constructors, and i dont want to use more function like CVector::constr1 which would use 3 floats or anything similiar, there must be a way to do it this way, could i get a bit of help? Thanks!
The problem is your use of default values for the arguments to two of the constructors, that means you have three constructors that can be called without arguments, so which should the compiler call?
Simply remove the default argument values and it should work.
Your class should be
class CVector
{
public:
CVector() : x(0), y(0), z(0) {}
CVector(float x) : x(x), y(0), z(0) {}
CVector(float x, float y) : x(x), y(y), z(0) {}
CVector(float x, float y, float z) : x(x), y(y), z(z) {}
//...
float x, y, z;
};
or
class CVector
{
public:
CVector(float x = 0, float y = 0, float z = 0) : x(x), y(y), z(z) {}
//...
float x, y, z;
};
Joachim Pileborg answered this perfectly, and deserves the points.
Here are the code changes you chould make:
CVector() // usage: CVector v();
{
this->x = 0, this->y = 0, this->z = 0;
}
CVector(const float x, const float y) { // usage: CVector v(1,2); // but z is always 0
this->x = x, this->y = y,this->z = 0;
}
CVector(const float x, const float y, const float z) { // usage: CVector v(1,2,3);
this->x = x, this->y = y, this->z = z;
}

C++ Error: "Multiple markers at this time: no matching function for call to" in constructor

Multiple markers at this line
- candidates are:
- no matching function for call to
'Coordinate::Coordinate()'
I am getting this error in the constructor of my class and I don't understand why. Here is the code involved:
RadialScan header
#ifndef RADIALSCAN_H_
#define RADIALSCAN_H_
#include "EasyBMP/EasyBMP.h"
#include <vector>
#include "Coordinate.h"
using namespace std;
class RadialScan {
vector<int> distanceTimeSeries;
vector<Coordinate> timeSeries;
BMP image;
Coordinate center;
Coordinate getNextPoint(Coordinate c);
bool isBlack(Coordinate c);
void computeTimeSeries();
public:
RadialScan(char* filename);
vector<int> getDistances();
vector<Coordinate> getCoordinates();
};
#endif
RadialScan class (all the methods are implemented, but the error is in the constructor and that's the code I'm providing):
#include "RadialScan.h"
RadialScan::RadialScan(char* filename){
image.ReadFromFile(filename);
int centerX = image.TellWidth()/2;
int centerY = image.TellHeight()/2;
center = Coordinate(centerX, centerY);
}
...
The error seems to be in the constructor. If I remove the constructor everything seems to compile correctly. If I delete the code inside the constructor I'm still getting the error. I don't understand why it keeps asking me for the Coordinate::Coordinate() constructor even when I don't have a coordinate object defined in the RadialScan(char* filename) constructor.
Additionally, these are the files for the Coordinate class:
header:
#ifndef COORDINATE_H_
#define COORDINATE_H_
class Coordinate {
int x;
int y;
public:
Coordinate(int x, int y);
void setX(int oneX);
void setY(int oneY);
int getX();
int getY();
double getMagnitude();
Coordinate operator-(const Coordinate&);
bool operator==(const Coordinate&);
Coordinate operator=(const Coordinate&);
};
#endif
cpp class:
#include "Coordinate.h"
#include <math.h>
Coordinate::Coordinate(int oneX, int oneY) {
x = oneX;
y = oneY;
}
//Setters
void Coordinate::setX(int oneX) {
x = oneX;
}
void Coordinate::setY(int oneY) {
y = oneY;
}
//Getters
int Coordinate::getX() {
return x;
}
int Coordinate::getY() {
return y;
}
double Coordinate::getMagnitude() {
return sqrt(x * x + y * y);
}
Coordinate Coordinate::operator-(const Coordinate& p) {
return Coordinate(x - p.x, y - p.y);
}
bool Coordinate::operator==(const Coordinate& p) {
return x == p.x && y == p.y;
}
Coordinate Coordinate::operator=(const Coordinate& p) {
return Coordinate(p.x, p.y);
}
Your constructor must look like
RadialScan::RadialScan(char* filename) : center (0, 0) {
image.ReadFromFile(filename);
int centerX = image.TellWidth()/2;
int centerY = image.TellHeight()/2;
center = Coordinate(centerX, centerY);
}
this because you did not implement default constructor and you can not create center object by default, so the only way is to call explicity Coordinate constructor with some default values.

C++ inheritance (overriding constructors)

I am learning OpenGL w/ C++. I am building the asteroids game as an exercise. I'm not quite sure how to override the constructors:
projectile.h
class projectile
{
protected:
float x;
float y;
public:
projectile();
projectile(float, float);
float get_x() const;
float get_y() const;
void move();
};
projectile.cpp
projectile::projectile()
{
x = 0.0f;
y = 0.0f;
}
projectile::projectile(float X, float Y)
{
x = X;
y = Y;
}
float projectile::get_x() const
{
return x;
}
float projectile::get_y() const
{
return y;
}
void projectile::move()
{
x += 0.5f;
y += 0.5f;
}
asteroid.h
#include "projectile.h"
class asteroid : public projectile
{
float radius;
public:
asteroid();
asteroid(float X, float Y);
float get_radius();
};
main.cpp
#include <iostream>
#include "asteroid.h"
using namespace std;
int main()
{
asteroid a(1.0f, 2.0f);
cout << a.get_x() << endl;
cout << a.get_y() << endl;
}
error I'm getting:
main.cpp:(.text+0x20): undefined reference to `asteroid::asteroid(float, float)'
You can use the : syntax to call the parent's constructor:
asteroid(float X, float Y) : projectile (x ,y);
Ok, just figured it out.
I actually don't have asteroid constructors defined because I thought they would inherit. But I think I have to do the following in asteroid.h:
asteroid(float X, float Y) : projectile(X, Y){];
You need a asteroid.cpp.
Even though inheriting from projectile, for non-default constructors (i.e., asteroid(float,float)), you still need to define the child class constructor.
You'll also need to define get_radius, as it's not defined in your base class.
Here's how that might look (I've taken the liberty of passing values for radius into both ctors):
#include "asteroid.h"
asteroid::asteroid(float r)
: projectile()
{
radius = r;
}
asteroid::asteroid(float x, float y, float r)
: projectile(x, y)
{
radius = r;
}
float asteroid::get_radius()
{
return radius;
}