Forward Declaration and Objects Question

Forward Declaration and Objects Question

Post by loveandhatecplusp.. » Mon, 05 Feb 2001 09:56:51



I would like to declare an iterator object within a list class and I am
unable.  The local university C++ professor was unable to find the
problem. Any gurus know the answer?  Code is below.  I am trying to
declare an iterator object within a list class and I am thus far unable.

Thanks.

#include <iostream>

using namespace std;
//*******************************
class Node
{//Node

private:
        int a;
        Node* next;

Quote:};

//******************************

class ListItr;//forward declaration

class List
{//list class
        friend class ListItr;//declare friend
public:
        //This line creates an error: "B uses undefined class 'ListItr'
        bool CreateList() { ListItr B(*this);};
private:
        Node* head;//head pointer

Quote:};//end list class

//*****************************

class ListItr
{//list iterator

public:
    //point iterator at list
        ListItr(List &A) {header = A.head;};
private:
        Node* header;

Quote:};//end iterator

//****************************

int main()
{
  return 1;

Quote:}

Sent via Deja.com
http://www.deja.com/
 
 
 

Forward Declaration and Objects Question

Post by Kenneth Ch » Mon, 05 Feb 2001 10:18:50




>I would like to declare an iterator object within a list class and I am
>unable.  The local university C++ professor was unable to find the
>problem. Any gurus know the answer?  Code is below.  I am trying to
>declare an iterator object within a list class and I am thus far unable.

>Thanks.

>#include <iostream>

>using namespace std;
>//*******************************
>class Node
>{//Node

>private:
>    int a;
>    Node* next;
>};
>//******************************

>class ListItr;//forward declaration

>class List
>{//list class
>    friend class ListItr;//declare friend
>public:
>    //This line creates an error: "B uses undefined class 'ListItr'
>    bool CreateList() { ListItr B(*this);};

Move this to after ListItr has been defined.  Remember to use the
inline keyword.

 
 
 

1. Is a friend declaration a forward declaration?

[snip]

The syntax of your friend declaration is wrong. And the definition of
the friend should precede the declaration in a template class that it
may be found during the lookup. Below is the example that demonstrates
the usage of friends:
// forward declare template class
template<class T> class A;
// define friend function template
template <class T>
int foo(A<T> a){ return a.i; }
// define class template
template <class T>
class A
{
  int i;
  public:
  A():i(0){}
  friend int foo<>(A<T>) // this uses template friend
  friend int foo() // this uses non-template friend
// define non-template friend
int foo(){ return 1; }

void foo()
{
  A a;
  // here is a test for your compiler
  std::cout<<"foo(a)="<<foo(a)<<"\nfoo()="<<foo();
  // you should see foo(a)=0 foo()=1

I verified this code with BCB4 and it worked as expected.
--
Gene Bushuyev


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. "etherfind" , ethernet watch..etc softwares on SGI??

3. Definition, Declaration and forward-declaration

4. Oh joy!

5. forward declaration question

6. DNS and Authentication

7. Questions about forward declarations with templates

8. Freecell hang when NVIDIA drivers are installed

9. Forward Declaration Question

10. Forward declaration

11. forward declaration?

12. Problem with forward declaration of class in header file

13. Forward declaration of class in a namespace ?