I have a class Vector that needs to have a function that returns type Point, and a class Point that needs to have a function that returns type Vector.

I was informed that I needed to use forward references, which I thought I had implemented as follows. I am still getting this error: use of undefined type 'Vector'

Here are my files:

Point.h

class Vector;

class Point
{

public:
	Vector GetVector()
	{
		Vector A;
		return A;
	}
};

Vector.h

class Point;

class Vector
{

public:
	Point GetPoint()
	{
		Point A;
		return A;
	}
};

Program.cpp

#include <iostream>

#include "Point.h"
#include "Vector.h"

using namespace std;

void main()
{
	Point P;
	Vector V;
}

Surely this is a very easy problem and I am just doing something silly. Please let me know.

Thanks!

Dave

Recommended Answers

All 8 Replies

Try following

[B]// Point.h[/B]
class Vector;
class Point
{
public:
	Vector GetVector();
};
//////////////////////////////////////////
[B]// Point.cpp[/B]
#include "point.h"
#include "vector.h"
Vector Point::GetVector()
{
    Vector v;
    return v;
}
//////////////////////////////////////////
[B]// Program.cpp[/B]
#include <iostream>
#include "Point.h"
#include "Vector.h"
using namespace std;
int main()
{
    Point P;
    Vector V;
    return 0;
}

Construct the vector.h/cpp in the same fashion.

You can not create an class object unless the class is fully defined. If all you have is a forward reference then all you can do is create a pointer of that type.

[edit]try ^^^^ mitrmkar's suggestion [/edit]

So basically all that is different is you put the functions in a cpp file?

I replaced

Point Vector::GetPoint()

with

Point GetPoint()

is that not a good idea?

Also, you include the headers in the Vector.cpp and Point.cpp AND in program.cpp - doesn't that mean you need to have #ifndef statements?

Thanks for the help!

GetPoint() still remains a method of the class, just put the code in a *.cpp file

// cpp file
Point Vector::GetPoint()
{
    // code here
}

>>doesn't that mean you need to have #ifndef statements?
Yes. But you should do that anyway just out of habbit

I replaced
Point Vector::GetPoint()
with
Point GetPoint()
is that not a good idea?

Hmm, a hard question for me to answer, it depends ... so to speak. But if your needs have remained unchanged, i.e. "I have a class Vector that needs to have a function that returns type Point, and a class Point that needs to have a function that returns type Vector.", you should stick with the construct described. Otherwise, you are free to find another solution that works for you the best.

"#ifndef statements" should naturally be involved (I was just illustrating the construct with forward declarations used as you needed, hence no #ifndef statements).

What I was saying was the compiler did not complain that I did NOT have #ifndef - shouldn't it have?
Other than that I think I'm good to go!

>>shouldn't it have?
Not necessarily. The compiler would have complained if Vector.h included Point.h and Point.h included Vector.h.

But I make it a habbit of using those code guards because (1) the header file looks naked without them and (2) never know when I might decided to include the header more than once.

Compilers do not insist on using #ifndef.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.