Thread: error c2036

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    7

    error c2036

    hello,

    i have a struct -

    Code:
    template <class T> 
    struct AnimalList
    {
    	list<T> myinfo;
    	AnimalList* next;
    };
    and that function -

    Code:
    template <class T> 
    AnimalList<T> Tuna::ageAndBreed() 
    {
    	AnimalList<T> g;
    	......
    	return g;
    }
    now i try call to the function

    Code:
    AnimalList<Animal*> l;
    l.myinfo.front()->ageAndBreed();
    and i get an error : 'struct AnimalList' : unknown size .

    what i do wrong ??

    tnx,
    Last edited by djons; 10-14-2008 at 05:31 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Thinking (meaning I didn't try this out to see whether it would work):
    should
    Code:
    AnimalList* next;
    be
    Code:
    AnimalList<T>* next;
    ?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    Quote Originally Posted by tabstop View Post
    Thinking (meaning I didn't try this out to see whether it would work):
    should
    Code:
    AnimalList* next;
    be
    Code:
    AnimalList<T>* next;
    ?
    it don't working,

    the program still not use in that value (so, you can to ignore it)

    Code:
    AnimalList* next;

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    What is the actual code. What is tuna? and what does it contain?

    maybe something like this is what your aiming for
    Code:
    template <class T> 
    struct AnimalList
    {
    	std::list<T> myinfo;
    	AnimalList* next;
    };
    
    template<typename T>
    class Tuna
    {
    public:
    	AnimalList<T> Tuna::ageAndBreed();
    
    };
    
    template <class T> 
    AnimalList<T> Tuna<T>::ageAndBreed() 
    {
    	AnimalList<T> g;
    	return g;
    }
    Last edited by Raigne; 10-14-2008 at 06:07 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    i tried it and it not work.

    what i need to do?
    i have an abstruct class - Animal
    Tuna and other animals inheritor Animal and implementation it.
    (Animal->Tuna)

    i need to creat a list of Animal - animalList (Animal* because it abstruct)

    how can i do it without
    include Animal.h in animalList ,and animalList.h in Animal
    together ,because it impossible.

    suggestion to solution or suggestion to fix the code accepted happily.

    tnx,

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you want to include animalList.h in Animal?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    because i have that function in Animal:

    AnimalList ageAndBreed()

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    how can i do it without
    include Animal.h in animalList ,and animalList.h in Animal
    together ,because it impossible.
    Why is that impossible?

    Animal.h
    Code:
    #ifndef ANIMAL_H_
    #define ANIMAL_H_
    //your header code
    #endif
    AnimalList.h
    Code:
    #ifndef ANIMALLIST_H_
    #define ANIMALLIST_H_
    //your head code
    #endif
    Last edited by Raigne; 10-14-2008 at 07:16 PM.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    but now -
    Animal can't use animalList
    and animalList can't use Animal
    because they don't know about each other....

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    How do they not?

    forward declare them

    Code:
    #ifndef ANIMAL_H_
    #define ANIMAL_H_
    
    class AnimalList;
    //your header code
    #endif
    AnimalList.h
    Code:
    #ifndef ANIMALLIST_H_
    #define ANIMALLIST_H_
    
    class Animal;
    //your head code
    #endif
    Last edited by Raigne; 10-14-2008 at 07:35 PM. Reason: add [/code] tag

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    It is not working

    i wrote

    #ifndef ANIMALLIST_H_
    #define ANIMALLIST_H_

    in Animal class, but it still don't know what is it?!

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Did you put the forward declarations?

  13. #13
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Which compiler are you using? Many compilers do not properly support templates, to the point that you will need to include your template class definitions in the same file as their implementation. So if your declarations are in Animal.h and implementation is in Animal.cpp, any file using Animal will need to include Animal.h and Animal.cpp.
    The best solution is to include your .cpp file IN your .h file, after your declarations (but rename it to something that the build won't pick up, like Animal.ch, so you don't get redefined symbols)

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    "It is not working" is not helpful. From your initial post, I suspect you've got some quite broken code, so please post more of it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    my code is ok.

    leave my code, here is an example of the same problem.

    this is the Abstract:


    Code:
    #include "C.h"
    #ifndef B_H
    #define B_H
    class B
    {
    public:
     B();
     virtual C EX()=0;
    };
    #endif

    Code:
    #include "B.h"
    #ifndef A_H
    #define A_H
    class A:public B
    {
    public :
     A();
     C EX();
    };
    #endif

    Code:
    #include <list>
    using namespace std;
    #ifndef C_H
    #define C_H
    template <class T>
    struct C
    {
     list<T> info;
    };
    #endif


    Code:
    #include "A.h"
    A::A();
    template <class T>
    C<T> A::EX()
    {
     C<int> l;
     return l;
    }


    #include "B.h"
    B::B(){};



    Code:
    #include "C.h"
    #include "A.h"
    using namespace std;
    void main()
    {
     C<B*> l;
     list<B*>::iterator iter;
     /*
     ...
      */
     for (iter = l.info.begin(); iter!=l.info.end(); ++iter)
     {
      (*iter)->EX();
     }
    }

    lets say that this is may all code.
    how can i fix the compiler error?
    this is the compiler error:
    error C2036: 'struct C' : unknown size

Popular pages Recent additions subscribe to a feed