-4

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;
    };
Kwon
  • 79
  • 1
  • 7
  • You don't need *both* a forward declaration *and* a header include. – Cory Kramer Dec 07 '18 at 17:28
  • Which means that the error lies with what's inside the header. – Matthieu Brucher Dec 07 '18 at 17:28
  • Do you have `#include "cpVector.hpp"` in `cpScalar.hpp`? – NathanOliver Dec 07 '18 at 17:29
  • Yes I have those. I edited my question to have cpScalar.hpp – Kwon Dec 07 '18 at 17:35
  • @CoryKramer I tried without header, but it still didn't work – Kwon Dec 07 '18 at 17:37
  • 1
    @Kwon Since `cpScalar.hpp` does not depend on `cpVector.hpp` remove the `#include "cpVector.hpp"` from it. Then you can remove `class cpScalar;` from `cpVector.hpp` and it should work. – NathanOliver Dec 07 '18 at 17:38
  • @NathanOliver that solved the problem for now! But eventually cpScalar needs to compute using cpVector as well. What should I do then? – Kwon Dec 07 '18 at 17:42
  • If you have a circular dependency then this is how you can solve it: https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes. I like the 2nd answers example the best. Ideally you should avoid the circular dependency if you can. – NathanOliver Dec 07 '18 at 17:43

2 Answers2

0

If you are new in c++, forward declaration might confuse you. You need to learn how the compiler reads files and how it determines linkages between them.

If that's the whole code of your cpScalar.hpp, then you dont need to include cpVector.hpp. It's because you are not instantiating or using the class in that file. Right now you have a circular dependency between the two classes. cpVector.hpp includes cpScalar.hpp, and then cpScalar.hpp includes cpVector.hpp. The compiler is your friend, be easy on him/her.

Camilo
  • 199
  • 2
  • 13
  • I edited my question - I actually do need that circular dependency and that's why I included those – Kwon Dec 07 '18 at 18:04
0

To clarify, cpVector relies on cpScalar and vice versa - circular dependency is needed

I see no reason to believe this. You may think a circular dependency is needed, but it's not. Think about how you learned about those concepts. You might have learned about scalars (in the form of counting things) way back before you started school. Vectors, on the other hand, tend to be a more advanced subject that built upon what you knew about scalars (in high school or maybe a few years earlier?).

The structure within a program is likely to be similar: scalars should be definable on their own, while vectors build upon scalars. When vectors and scalars interact (such as multiplying a vector by a scalar), the definition should belong to the more "advanced" class, i.e. cpVector. No circular dependency is needed.

JaMiT
  • 14,422
  • 4
  • 15
  • 31