Thread: need help with my .h, .cpp, and a class

  1. #1
    Unregistered
    Guest

    Question need help with my .h, .cpp, and a class

    Okay, i wanted to create a header file that had prototypes for a cpp file(file 1). The cpp file consists of a class(class 1) and its member functions. Both files have been debugged.

    Now, i want to use that class in a new program(file 2). So, i made sure to include my header file. In file 2, i have a class(class 2)...
    and i want to declare a private instance of class 1 within it. So, this is what i put in file 2...

    ...
    const int num = 5;

    class class2
    {
    private:
    class1 name(num);
    public:
    ...some declarations...
    };

    ...

    NOTE: The class1 constructor takes one integer argument

    When i try to compile, i get these errors:
    ----------------
    error C2146: syntax error : missing ';' before identifier 'name'
    error C2501: 'class1' : missing storage-class or type specifiers
    error C2059: syntax error : 'constant'
    ----------------

    How can i get this to work? Any help would be appreciated.. thnx

    -GhostAgent

  2. #2
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    error C2059: syntax error : 'constant'

    what ever your defining as constant, should be like this:
    Code:
    const A;
    not
    Code:
    constant A;
    the best things in life are simple.

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    the error is most likely in the file1, so it would be easier if you would post both files ......

    and 1 more question:

    the error happens while the compiler runs right???

  4. #4
    Unregistered
    Guest
    I actually dont have an const's... the argument was a number, i wrote const for clarity... but anyway, heres a more accurate representation:

    ----------------------------------------------
    --------------------------------header file:

    #ifndef HEADERFILE_H
    #define HEADERFILE_H

    #include <file1.cpp>

    class1(arg);

    void func(arg, arg);
    void func();

    #endif

    --------------------------------file1:

    class class1
    {
    private:

    ...funcs and vars...

    public:

    class1(arg);
    void func(arg, arg);
    void func();
    };

    class1::class1(arg argName)
    {

    ...code...

    }

    void class1::func(arg argName, arg argName)
    {

    ...code...

    }

    ...other functions...

    --------------------------------file2:

    #include <headerfile.h>

    class class2
    {
    private:

    class1 className(arg); // <--- does this work?
    ...funcs and vars...

    public:

    class2();
    ...funcs and vars...
    }

    ...class2 constructor and other functions...
    -----------------------------------------------------

    Am i going to have to use inheritence? the only thing is, i want class1 to be a member class of class2, without changing the code of file1.

    -GhostAgent

  5. #5
    Unregistered
    Guest
    Well, i fixed my problem... it was in the header file.

    This is what i had for a protype...

    ----------------------------------------
    void memberFunc(arg);
    ----------------------------------------

    ...When i should have had this...

    ----------------------------------------
    void class::memberFunc(arg);
    ----------------------------------------

    ...Simple mistake, big headache: solved.

    -GhostAgent

  6. #6
    Unregistered
    Guest
    hm... now I dont know how(where?) to declare an instance of class1 in class2.

Popular pages Recent additions subscribe to a feed