Thread: Including a custom class file

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    Including a custom class file

    Hey,

    I have two files. One is a class file that I created and the other file implements the class. I have a couple of questions concerning this.

    First off, in my class file, do I need to include the class declaration and all the methods of the class? Right now my class file just looks like this:

    Code:
    class myStack {
        
        public:
            string (*aa);
            int stackSize;
            int cc;
            void setStackSize(int);
            void pop(int);
            void push(int);
            int displayStackSize();
    };
    The methods setStackSize, pop, and push are all written inside of my driver program.

    Second, to include my class from my driver program, is the correct code:
    Code:
    #include "myStack"
    or does it need a .c or a .h at the end? .h is only for header files, what is a .c? Does that just mean that it is written in C?

    Thanks!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your class file (header) should also have inclusion guards
    Code:
    #ifndef SOMETHING_H
    #define SOMETHING_H
    
    class YourClass {};
    
    #endif
    Google to see what inclusion guards are needed for.

    To include the custom header files, use #include "myheader.h" (provided you have saved it with the ".h" extension). I don't think file extensions mean a lot per se - they're all simple text files after all - but certain programs, such as IDE's and compilers (?) use them to automatically figure out what kind of files these are.

    By the way, what is
    Code:
    string (*aa);
    ?

    If it is a string pointer, one conventionally wouldn't use the braces. But you probably don't need a pointer anyway.

    And to use a string, the header should #include <string> and make sure the name is looked up in std namespace: it's std::string.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    So can I put my class methods in the same file as the class? Or do I just define the class in one file and have the methods in the driver file? And then I would use #include myStack.cpp inside my driver file to include the class file if I saved it as myStack.cpp ?

    the string (*aa) is a string pointer to an array.

    edit: i've been reading more around this forum. Is this on the right track?

    myStack.h - the class definition
    myStack.cpp - the class methods, includes myStack.h
    stackDriver.cpp - "drives" the program, includes myStack.cpp
    Last edited by Beowolf; 09-25-2007 at 06:51 PM.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Beowolf View Post
    So can I put my class methods in the same file as the class? Or do I just define the class in one file and have the methods in the driver file? And then I would use #include myStack.cpp inside my driver file to include the class file if I saved it as myStack.cpp ?

    the string (*aa) is a string pointer to an array.

    edit: i've been reading more around this forum. Is this on the right track?

    myStack.h - the class definition
    myStack.cpp - the class methods, includes myStack.h
    stackDriver.cpp - "drives" the program, includes myStack.cpp
    Almost, but you have to actually include myStack.h in your stackDriver.cpp file. It's a bit complicated

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To confirm what QuantumPete says: It is almost never ever right to include a .cpp (or .c) file.

    The idea is that you compile several .cpp files and combine them in a process called "linking". Header files (.h) are used to let each .cpp file know what functions, classes and variables are declared in other .cpp files, so that each .cpp file can make use of the other .cpp files functions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    It is almost never ever right to include a .cpp (or .c) file.
    Though it does happen and those people need to be poked with a sharp stick.

    To build on what matsp said:
    Each .c(pp) file is compiled by itself. If you are using a function (or class, etc) that is defined in another .c file, then you don't have a definition for it in the current file and the compiler thinks it's undefined and has no code -> boom, compiler won't let you do that. If you include a declaration, however (such as you have in .h files, that's what they're there for), then you tell the compiler: 'hold on! I know the code for that function isn't in this file, but here's what it should look like and don't worry, the linker will find it'.
    The linker then takes all your source files and puts them into a great big program (or library or whatever), whilst doing so, it searches all source files that are included for any functions that are undefined in a particular source file.
    Since this not only applies to functions, but also to classes, basic types, etc, we generally refer to symbols.

    Hope that's not *more* confusing now.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    Though it does happen and those people need to be poked with a sharp stick.
    Yes, I agree.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Ok, I broke my program down into three files, but I am getting some compiler errors. First, my files:

    myStack.h
    Code:
    /* myStack.h */
    #include <string>
    
    class myStack
    {
        public:
            string (*aa);
            int stackSize;
            int cc;
            void setStackSize(int);
            void pop(int);
            void push(int);
            int displayStackSize();
    };
    myStack.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "myStack.h"
    
    using namespace std;
    
    void myStack::setStackSize(int x) 
    {
    
        stackSize = x;
    
        if(!(aa = new(nothrow) string[stackSize+1])) 
            cerr << "Program cannot handle a stack of size " << stackSize << ".\n";  
       
    }
    
    
    void myStack::pop(int p) {
        if(p == 0)
            cout << "Empty stack.\n";
        else
            cout << aa[p-1] << "\n";
    }
    
    
    void myStack::push(int p) 
    {
        if(p == stackSize) 
        {
            cerr << "Full stack.\n";
        } 
        else 
        {
            aa[p] = aa[p].substr(1, aa[p].length());
            cc++;
        }
    }
    stackDriver.cpp
    Code:
    #include <iostream>
    #include <string>
    #include <ctype.h>
    #include <stdlib.h>
    #include "myStack.h"
    
    using namespace std;
    
    int main(int argc, char *argv[]) {
        myStack stack1;
        int arg_length;
        char current_digit;
    
    
        if(argc < 2) {
            cerr << "Bad command line argument.\n";
            exit(1);
        }
    
        arg_length = strlen(argv[1]);
    
        if(arg_length > 9) {
            cerr << "Bad command line argument.\n";
            exit(1);
        }
        
        for(int ii = 0; ii < arg_length; ii++) {
            current_digit = argv[1][ii];
            if(!(isdigit(current_digit))) {
                cerr << "Bad command line argument.\n";
                exit(1);
            }
        }
    
        stack1.setStackSize(atoi(argv[1]));
    
        stack1.cc = 0;
        while(1 != 2) { // infinite loop
            getline(cin, stack1.aa[stack1.cc]);
            if(stack1.aa[stack1.cc].length() > 80)
                cerr << "Bad input.\n";
            else if(stack1.aa[stack1.cc][0] == '<')
                stack1.pop(stack1.cc);
            else if(stack1.aa[stack1.cc][0] == '>')
                stack1.push(stack1.cc);
            else if(stack1.aa[stack1.cc].length() == 0)
                exit(0);
            else
                cerr << "Bad input.\n";
        }      
        
    
        system("Pause");
        return 0;
    }
    Sorry for the long code postings, but I thought you would probably want to see the whole thing. My compiler errors are this:

    Code:
    3 N:\USERS\bakewens\CS240\Program 2\myStack.cpp In file included from N:\USERS\bakewens\CS240\Program 2\myStack.cpp 
    7 N:\USERS\bakewens\CS240\Program 2\myStack.h ISO C++ forbids declaration of `string' with no type 
    7 N:\USERS\bakewens\CS240\Program 2\myStack.h expected `;' before '(' token
    and then a lot more, but they all have to do with not having aa initialized, and that is because of the declaration of 'string' with no type error.

    So, two questions: first, of course, what is the compiler complaining about "ISO C++ forbids a declaration of 'string' with no type'? What does that mean?

    Second, is my file structure finally correct? :-p

    Thanks for all of the help!

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It should be:
    Code:
    std::string *aa;
    which is actually what anon said in the first reply.

    >> what is the compiler complaining about "ISO C++ forbids a declaration of 'string' with no type'?
    Since you didn't specify the std namespace, the compiler didn't know what "string" was.

    >> is my file structure finally correct?
    You need header include guards in your header file (see anon's reply). Everything else looks fine.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Doh! Thanks.

    I just compiled myStack.cpp again and the only thing that I received was this error that I have never seen before:

    Code:
     [Linker error] undefined reference to `WinMain@16' 
    ld returned 1 exit status

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That is a linker error. Both myStack.cpp and stackDriver.cpp should be in the same project (are you using Dev-C++?).

    If they're in the same project, both will be compiled and linked to each other, and the linker will find the main() function in stackDriver.cpp

    If you try to run myStack.cpp by itself or have it in its own project, the linker will complain because you cannot create an executable without the main function.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    630
    Your compiler wants WinMain() instead of main().

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Your compiler wants WinMain() instead of main().
    I think it mentions WinMain even though it would be perfectly happy with a regular main. I might be wrong about this though.

    If it does insist on a WinMain, then the project is wrong. You should be using a console application project, not a windows application project.

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Yes, I am using Dev-C++.

    I've put my three files in a project now. The structure is like:

    Project1:
    --main.cpp
    --myStack.cpp
    --myStack.h

    I went to compile the project and now it is giving me this error:
    Code:
    1 N:\USERS\bakewens\CS240\Program 2\myStack.cpp expected unqualified-id before '[' token 
    1 N:\USERS\bakewens\CS240\Program 2\myStack.cpp expected `,' or `;' before '[' token
    Code:
    Code:
    #include <iostream> //problem
    #include <string>
    #include "myStack.h"
    
    using namespace std;
    
    void myStack::setStackSize(int x) 
    [etc]
    How does this whole project thing work when I want to run my program on another computer? For example, if I take my 3 files and want to compile them in another place that is not running Dev-C++, is it going to give me the WinMain error again?

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm not sure why it gives you that error, is that the entire error text? Does it give you a line number or some other error message? Can you give more details on how you created the project?

    As far as running on another computer, how you do it depends on what compiler or IDE is on the other computer. If you have Dev-C++ on the other computer, you can create a console project and add the files to it like you did here. There might be a project file that Dev-C++ creates and you can just copy that over, but I'm not familiar enough with Dev-C++ to know.

    If you're using a different IDE or compiler, then you create a project or makefile or command line that is appropriate for that compiler. In VC++, you'd create an empty Win32 Console Application project and add the files to the project. If you're using gcc without an IDE, then you'd compile the two cpp files and link them together on the command line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM