i cave this code

#include <iostream>
#include <cstdlib>
#include <cassert>

using namespace std;

typedef int typos_akmis;



template <class typos_stoixeiou>
struct korifi
{
	typos_stoixeiou dedomena;
       /* i would like to do korifi<typos_stoixeiou> * ->kdktis */
	korifi<typos_stoixeiou> * epomenos;   
       akmi<typos_stoixeiou>* kefali;   /* here is the problem */
	int episkeftike; 
} ;




template <class typos_stoixeiou>
struct akmi
{
	typos_akmis dedomena;  
	korifi<typos_stoixeiou> * akro;  
	akmi<typos_stoixeiou>* epomenos;
	int simadi; 
};



template <class typos_stoixeiou>
class grafos
{
    public:
	  typedef struct korifi<typos_stoixeiou> *kdktis;
          typedef struct akmi<typos_stoixeiou> *adktis;
	   typedef kdktis graphima;
           graphima G;
	public:
              //All the functions
};

can someone tell me a solution to bypass these problems?
Thank you very much

Dani AI

Generated

The compiler error comes from a mutual dependency: korifi<T> holds a pointer to akmi<T> while akmi<T> holds a pointer back to korifi<T>. A forward declaration of the akmi template (as pointed out) lets the compiler know the name exists before it is used inside korifi. 's stylistic point is also worth keeping in mind: prefer clear, minimal naming and avoid hiding important semantics with needless typedefs.

A clean, modern approach is to forward-declare the edge type, then define the node (which can safely store a pointer to the edge), and finally define the edge. The pattern below shows the structure (different identifiers are used to avoid reproducing the post verbatim):

template <typename NodeT, typename EdgeT = int> struct Edge;
template <typename NodeT, typename EdgeT = int> struct Node;

template <typename NodeT, typename EdgeT>
struct Node {
    NodeT data;
    Node<NodeT,EdgeT>* next;
    Edge<NodeT,EdgeT>* firstEdge;
};

template <typename NodeT, typename EdgeT>
struct Edge {
    EdgeT data;
    Node<NodeT,EdgeT>* target;
    Edge<NodeT,EdgeT>* next;
};

Additional practical notes:

  • Prefer using aliases (C++11+) over typedef for readability, and avoid typedefing raw pointer types if it hides ownership semantics.
  • If an integer index is used purely for counts/indices, consider std::size_t or make it a template parameter rather than a fixed int.
  • Avoid using namespace std; in headers.
  • Place forward declarations and definitions in the same header (or split clearly into a header with forward declarations and an implementation header) with include guards to prevent ODR/visibility problems.

These changes remove the circular-definition compile errors and give a clearer, more extensible design than typedef-ing pointer aliases and keeping types in ambiguous order.

Recommended Answers

All 2 Replies

You need a forward decleration. Put this at the top of your file :

template <class typos_stoixeiou>
struct akmi;

template <class typos_stoixeiou>
struct korifi{ ...  }

Dude, i really wanna help but one rule you should always have in mind if u want to be a professional programmer is to simplify your code as best as possible, so i do not see any reason why you have to typedef int to some weird variable name when the word int is so preferrable compared to it.

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.