I have two header file Fibonaci.h, Fibonaci_Iterator.h, u can look below

Fibonaci.h

#ifndef __fibonaci__
#define __fibonaci__

#include "stdafx.h"
#include "Fibonaci_Iterator.h"

using namespace std;

class Fibonaci
{
	public:
		//typedef Fibonaci_Iterator iterator;						
		Fibonaci_Iterator begin();	-->	[B]syntax error missing ; before begin[/B]
		Fibonaci_Iterator end();

		Fibonaci( const int& );
		Fibonaci( const int&, const int& );
		bool isElem( const int& );
		void genElem();
		void genElemToValue( const int& );
		void printElem();				
		
		static vector<int> _elems;		
	private:
		friend int Fibonaci_Iterator::operator*(); --> [B]Fibonaci_Iterator is not a class or namespace name[/B]
		friend void Fibonaci_Iterator::checkValidity() const;
		int _length;
		int _beg_pos;
};

#endif

Fibonaci_Iterator.h

#ifndef __fibonaci_iterator__
#define __fibonaci_iterator__

#include "Fibonaci.h"

class Fibonaci_Iterator
{
	public:			
		Fibonaci_Iterator( const int& );		
		bool operator==( const Fibonaci_Iterator & ) const;
		bool operator!=( const Fibonaci_Iterator & ) const;
		Fibonaci_Iterator& operator++();
		Fibonaci_Iterator operator++( int );
		int operator*();
		void checkValidity() const;
	private:
		int _index;
};

#endif

When i compile, i get a lot of error, i put some above.
In first error syntax error missing ; before begin, why did i get this error in function Fibonaci_Iterator begin(); --> i make function with return value class Fibonaci_Iterator and Ive put header Fibonaci_Iterator.
In the second error Fibonaci_Iterator is not a class or namespace name, how to resolve this???

Recommended Answers

All 7 Replies

Can anyone help me???? :((

The problem seems to be nested includes. Each header files includes the other header file, so neither header is completly processed before the class is used in the other header. To avoid that problem predeclare the class. See the example below

Second: stdafx.h is not to be included in header files. This file must be included at the top of *.cpp files and must appears as the first include file. If you don't do this and you compile with precompiled headers then your program will not compile.

#ifndef __fibonaci__
#define __fibonaci__

#include "stdafx.h" // <<< this is not necessary in header files 

#include "Fibonaci_Iterator.h"

class Fibonaci_Iterator; // <<<<<<<<<<<<<<<<<< here

using namespace std;

class Fibonaci
// rest of header file here

Even if you make the above change you are going to have fatal problems because pre-declared classes can only appear as a pointer since the class has not been fully declared to the compiler. Looking over your code I think the easiest way to solve your problem is to remove line 4 from Fibonaci_Iterator.h

Do you have a source file?

I still cant solve this.
Maybe I show u more:

//Fibonaci.h

#ifndef __fibonaci__
#define __fibonaci__

#include "stdafx.h"
#include "Fibonaci_Iterator.h"

using namespace std;

class Fibonaci
{
	public:
		//typedef Fibonaci_Iterator iterator;						
		Fibonaci_Iterator begin();		
		Fibonaci_Iterator end();		

		Fibonaci( const int& );
		Fibonaci( const int&, const int& );
		bool isElem( const int& );
		void genElem();
		void genElemToValue( const int& );
		void printElem();								

		static std::vector<int> _elems;		
	private:
		friend int Fibonaci_Iterator::operator*();
		friend void Fibonaci_Iterator::checkValidity() const;
		int _length;
		int _beg_pos;
};

//Fibonaci.cpp
#include "Fibonaci.h"
vector<int> Fibonaci::_elems;

Fibonaci::Fibonaci(const int &length = 1)
:	_length( length )
{ .... }
Fibonaci_Iterator Fibonaci::begin()
{
	return Fibonaci_Iterator( 0 );
}
.....

//Fibonaci_Iterator.h
#ifndef __fibonaci_iterator__
#define __fibonaci_iterator__

#include "Fibonaci.h"

class Fibonaci_Iterator
{
	public:			
		Fibonaci_Iterator( const int& );		
		bool operator==( const Fibonaci_Iterator & ) const;
		bool operator!=( const Fibonaci_Iterator & ) const;
		Fibonaci_Iterator& operator++();
		Fibonaci_Iterator operator++( int );
		int operator*();
		void checkValidity() const;
	private:
		int _index;
};

#endif

//Fibonaci_Iterator.cpp
#include "Fibonaci_Iterator.h"

Fibonaci_Iterator::Fibonaci_Iterator(const int &index)
:	_index(index)
{}
void Fibonaci_Iterator::checkValidity() const
{
	if( _index > Fibonaci::_elems.size() )
		cerr << "Index is out of range";	
}
int Fibonaci_Iterator::operator *()
{
	checkValidity();
	return Fibonaci::_elems[_index];
}

Maybe error happen because i dont know how to put header correctly.

In the .cpp:

#include <vector>
using std::vector;
#include "Fibonaci.h"
#include "Fibonaci_Iterator.h"
#include <iostream>
using std::cerr;

Finally I have successfully compile itu, i remove #include "Fibonaci.h" in Fibonaci_Iterator.h to
Fibonaci_Iterator.cpp.

TQ all.

Finally I have successfully compile itu, i remove #include "Fibonaci.h" in Fibonaci_Iterator.h to
Fibonaci_Iterator.cpp.

TQ all.

Wrong move. You should NEVER EVER include a *.cpp in another *.cpp or *.h file

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.