I'm new to c++ and am trying to make calculations using two .hpp files cpScalar.hpp and cpVector.hpp as assignment. I'm having difficulty as I read through forward declarations explanations - all solutions I find say that I cannot use methods from another class in another header before "declaring class" fully, and I don't know what I have to do to "fully declare/ define" the class.
To clarify, cpVector relies on cpScalar and vice versa - circular dependency is needed
I'm planning to use of cpScalar to get array of cpScalar in cpVector, but I cannot access parameter input 'cpScalar sarr[]' because I did not declare, and am getting invalid use of incomplete type error. I want to know what I need to do for this section.
I don't intend to use pointer in place of vectors in constructors, as this leads to flexible array problem that are (seemingly) solved using 'struct' and 'malloc' which I did not learn in class.
Below is my code:
// cpVector
#ifndef CPVECTOR_HPP
#define CPVECTOR_HPP
#include <iostream>
#include <vector>
#include "cpScalar.hpp"
using namespace std;
class cpScalar;
class cpVector{
private:
vector<cpScalar> arr; // cpScalar* arr; seems to be more complicated...
unsigned int size;
public:
cpVector(cpScalar sarr[], unsigned int size2){ // this constructor is given
this->size = size2;
arr.resize(size);
for (int i =0; i<size; i++){
arr[i] = sarr[i]; // this gives incomplete type error
}
};
... more public functions...
#endif
#ifndef CPSCALAR_HPP
#define CPSCALAR_HPP
#include <iostream>
#include <string>
#include "cpVector.hpp"
using namespace std;
class cpVector;
class cpScalar{
private:
int intScalar;
double doubScalar;
public:
cpScalar(int num){
intScalar = num;
};
cpScalar(double num){
doubScalar = num;
};