Compilation Error:Class name does not name a type.

Discussion in 'C++' started by sharat, Apr 19, 2007.

  1. sharat

    sharat Guest

    hi groups,

    i have created 2 .cpp and .h(header) file and one main.cpp file, such
    as file1.cpp and .h similarly file2.cpp & .h .
    i have include the apropriate header files also .now i am creating the
    object of class A (which is declare and define in file1.h) into class
    B(which is declare and define in file2.h). i am getting an error that
    "class a " doesnot name a type and the object i have creted is
    undeclared . i am unable to get why this error is coming ,as i have
    includede the related header files also.

    can any body help to solve this problem.

    Thanx in advance...
     
    sharat, Apr 19, 2007
    #1
    1. Advertising

  2. sharat

    Ian Collins Guest

    sharat wrote:
    > hi groups,
    >
    > i have created 2 .cpp and .h(header) file and one main.cpp file, such
    > as file1.cpp and .h similarly file2.cpp & .h .
    > i have include the apropriate header files also .now i am creating the
    > object of class A (which is declare and define in file1.h) into class
    > B(which is declare and define in file2.h). i am getting an error that
    > "class a " doesnot name a type and the object i have creted is
    > undeclared . i am unable to get why this error is coming ,as i have
    > includede the related header files also.
    >
    > can any body help to solve this problem.
    >

    Please post a minimal example that gives your problem. Otherwise all
    you can hope for is a guess.

    --
    Ian Collins.
     
    Ian Collins, Apr 19, 2007
    #2
    1. Advertising

  3. sharat

    Salt_Peter Guest

    On Apr 19, 2:28 am, sharat <> wrote:
    > hi groups,
    >
    > i have created 2 .cpp and .h(header) file and one main.cpp file, such
    > as file1.cpp and .h similarly file2.cpp & .h .
    > i have include the apropriate header files also .now i am creating the
    > object of class A (which is declare and define in file1.h) into class
    > B(which is declare and define in file2.h). i am getting an error that
    > "class a " doesnot name a type and the object i have creted is
    > undeclared . i am unable to get why this error is coming ,as i have
    > includede the related header files also.
    >
    > can any body help to solve this problem.
    >
    > Thanx in advance...



    Not unless you show code.
    Note that < class A > and < class a > do not correspond.
    So the error is probably a fundamental one.
     
    Salt_Peter, Apr 19, 2007
    #3
  4. sharat

    sharat Guest

    here is my code ..

    //file1.cpp
    using namespace std;
    #include "file1.h"
    void plot_class::fun1()
    {
    cout<<"\n PLOT CLASS \n";
    }

    //file1.h
    #ifndef FILE1_H
    #define FILE_H

    #include"file2.h"
    class plot_class
    {
    public:
    int p;
    zone_class zone_obj;
    void fun1();

    };
    #endif

    similarly ,
    //file2.cpp
    #include"file2.h"
    void zone_class ::fun_zone()
    {
    cout<<"\n zone class \n";
    }

    //file2.h
    #ifndef FILE2_H
    #define FILE2_H
    #include"file1.cpp"

    class zone_class
    {
    public:
    int z;
    plot_class plot_obj;
    void fun_zone();
    };
    #endif

    and main.cpp is as follows.

    #include<iostream>
    #include"file1.cpp"
    #include"file2.cpp"
    using namespace std;
    int main()
    {
    cout<<"\n Hello World\n";
    zone_class zone_obj1;

    zone_obj1.fun_zone();

    }

    i am using gcc compiler ..after compiling i am getting the following
    errors
    $ g++ main.cpp


    file2.h:9: error: 'plot_class' does not name a type
     
    sharat, Apr 19, 2007
    #4
  5. * sharat:
    > here is my code ..
    >
    > //file1.cpp
    > using namespace std;
    > #include "file1.h"


    Preferentially include the header file before /anything/ else, so that
    you get a check (however fallible) that the header file is OK on its own.


    > void plot_class::fun1()
    > {
    > cout<<"\n PLOT CLASS \n";
    > }


    You haven't included <iostream> and <ostream>. OK, you have, in the
    main program, but that's a bug masking a bug.


    > //file1.h
    > #ifndef FILE1_H
    > #define FILE_H


    Typo.


    > #include"file2.h"
    > class plot_class
    > {
    > public:
    > int p;
    > zone_class zone_obj;
    > void fun1();
    >
    > };
    > #endif
    >
    > similarly ,
    > //file2.cpp
    > #include"file2.h"
    > void zone_class ::fun_zone()
    > {
    > cout<<"\n zone class \n";
    > }
    >
    > //file2.h
    > #ifndef FILE2_H
    > #define FILE2_H
    > #include"file1.cpp"


    Typo or thinko.

    Don't include implementation files, ever.


    > class zone_class
    > {
    > public:
    > int z;
    > plot_class plot_obj;
    > void fun_zone();
    > };
    > #endif
    >
    > and main.cpp is as follows.
    >
    > #include<iostream>
    > #include"file1.cpp"


    Don't include implementation files, ever.


    > #include"file2.cpp"


    Don't include implementation files, ever.


    > using namespace std;
    > int main()
    > {
    > cout<<"\n Hello World\n";
    > zone_class zone_obj1;
    >
    > zone_obj1.fun_zone();
    >
    > }
    >
    > i am using gcc compiler ..after compiling i am getting the following
    > errors
    > $ g++ main.cpp
    >
    >
    > file2.h:9: error: 'plot_class' does not name a type


    In [main.cpp] you include first of all [file1.cpp], which includes
    [file1.h], which includes [file2.h], which at line references
    plot_class, which so far hasn't been declared or defined.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Apr 19, 2007
    #5
  6. Le 19.04.2007 11:02, sharat a ecrit:

    > here is my code ..
    >
    > class plot_class
    > {
    > public:
    > int p;
    > zone_class zone_obj;
    > void fun1();
    >
    > };


    So, a plot_class contains an int and a zone_class...

    > class zone_class
    > {
    > public:
    > int z;
    > plot_class plot_obj;
    > void fun_zone();
    > };


    So, a zone_class contains an int and a plot_class...

    Question: assuming an int is 4 bytes and there is no extra byte wasted,
    can you compute the size of a plot_class? the size of a zone_class?

    Do you see your problem now?

    --
    ___________
    _/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
    \ \_L_) Pour bien répondre avec Google, ne pas cliquer
    -'(__) « Répondre », mais « Afficher les options »,
    _/___(_) puis cliquer « Répondre » (parmi les options).
     
    Serge Paccalin, Apr 19, 2007
    #6
  7. sharat

    anon Guest

    Serge Paccalin wrote:
    > Le 19.04.2007 11:02, sharat a ecrit:
    >
    >> here is my code ..
    >>
    >> class plot_class
    >> {
    >> public:
    >> int p;
    >> zone_class zone_obj;
    >> void fun1();
    >>
    >> };

    >
    > So, a plot_class contains an int and a zone_class...
    >
    >> class zone_class
    >> {
    >> public:
    >> int z;
    >> plot_class plot_obj;
    >> void fun_zone();
    >> };

    >
    > So, a zone_class contains an int and a plot_class...
    >
    > Question: assuming an int is 4 bytes and there is no extra byte wasted,


    Extra byte wasted?

    > can you compute the size of a plot_class? the size of a zone_class?
    >


    How can it matter?

    > Do you see your problem now?
    >


    I don't
     
    anon, Apr 19, 2007
    #7
  8. Le 19.04.2007 11:16, anon a ecrit:

    > Serge Paccalin wrote:
    >
    >> Question: assuming an int is 4 bytes and there is no extra byte wasted,

    >
    > Extra byte wasted?


    For alignment purposes, for instance. "Wasted" is probably not the
    correct word, but English is not my native language either.

    >> can you compute the size of a plot_class? the size of a zone_class?

    >
    > How can it matter?


    Just try it.

    >> Do you see your problem now?
    >>

    >
    > I don't


    Circularity.

    --
    ___________
    _/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
    \ \_L_) Pour bien répondre avec Google, ne pas cliquer
    -'(__) « Répondre », mais « Afficher les options »,
    _/___(_) puis cliquer « Répondre » (parmi les options).
     
    Serge Paccalin, Apr 19, 2007
    #8
  9. sharat

    anon Guest

    Serge Paccalin wrote:
    > Le 19.04.2007 11:16, anon a ecrit:
    >
    >> Serge Paccalin wrote:
    >>
    >>> Question: assuming an int is 4 bytes and there is no extra byte wasted,

    >> Extra byte wasted?

    >
    > For alignment purposes, for instance. "Wasted" is probably not the
    > correct word, but English is not my native language either.
    >

    I do not see where a byte can be wasted there, and how can it influence
    the error he is getting.


    >>> can you compute the size of a plot_class? the size of a zone_class?

    >> How can it matter?

    >
    > Just try it.
    >
    >>> Do you see your problem now?
    >>>

    >> I don't

    >
    > Circularity.
    >


    Do you mean circularity with includes or what?
    He should have declared classes, instead of including header files
    (including source files is out of question here)
    Like this:

    //file1.h
    #ifndef FILE1_H
    #define FILE1_H

    class zone_class;

    class plot_class
    {
    public:
    int p;
    zone_class zone_obj;
    void fun1();

    };
    #endif

    Similar for file2.h
     
    anon, Apr 19, 2007
    #9
  10. sharat

    Ian Collins Guest

    anon wrote:
    >
    > Do you mean circularity with includes or what?


    class A has a member of type class B. Class B has a member of type
    class A. How big is class A?

    --
    Ian Collins.
     
    Ian Collins, Apr 19, 2007
    #10
  11. sharat

    anon Guest

    Ian Collins wrote:
    > anon wrote:
    >> Do you mean circularity with includes or what?

    >
    > class A has a member of type class B. Class B has a member of type
    > class A. How big is class A?
    >


    Thanks for clarifications :)
    I knew I have used something similar, but references instead of objects
     
    anon, Apr 19, 2007
    #11
  12. sharat

    sharat Guest

    still not clear
     
    sharat, Apr 19, 2007
    #12
  13. * sharat:
    > still not clear


    In C++, if an object A contains another object B, it really directly
    contains object B.

    The size of object A is the size of its member object B plus the sizes
    of whatever other members there are in object A, pluss the size of
    compiler-inserted padding, if any.

    Now, let type Y have some data member such that its size is at least 1.
    If any object of type X contains an object of type Y, which in turn
    contains an object of type X, which..., what's the size of an object of
    type X? What's the size of an object of type Y?

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Apr 19, 2007
    #13
  14. sharat

    anon Guest

    sharat wrote:
    > still not clear
    >


    You need to use pointers or references, instead of actual objects inside
    your classes. Like in these headers:

    //file1.h
    #ifndef FILE1_H
    #define FILE1_H

    class zone_class;
    class plot_class
    {
    public:
    int p;
    zone_class& zone_obj;
    void fun1();

    };
    #endif

    //file2.h
    #ifndef FILE2_H
    #define FILE2_H

    class plot_class;
    class zone_class
    {
    public:
    int z;
    plot_class& plot_obj;
    void fun_zone();
    };
    #endif

    Otherwise, object of class zone_class would contain an object of
    plot_class class, which contains object of zone_class class, ... , to
    infinity.
     
    anon, Apr 19, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page