Thread: Classes

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    6

    Classes

    Im pretty sure, this has allready been answered, but I used the search and i couldn't find anything, so...
    Im new to c++ but not new in programming.

    I need several procedures (probably around 50), that I would like to group in the same classes. For example, file related procedures in one class, math related in the other... and have one class per file.
    The problem is, I have no idea, how to do that.

    At the moment, I have each class in its own cpp file, each cpp file has a header file and a file with main function.

    This is bascly how things look like:

    files.h
    Code:
    #ifndef FILES_H
    #define FILES_H
    class files{
    	public:
    		static bool exists(char *path[]);
    };
    #endif
    files.cpp
    Code:
    #include "files.h"
    
    static bool exists(char *path[]){
    	//code
    	return false;
    }
    main.cpp
    Code:
    #include <stdio.h>
    #include "files.h"
    
    int main(int argc, char *argv[])
    {
    	printf("\n");
    	//were is where i'd like to use the function
    	return 0;
    }
    Last edited by Grim; 03-28-2007 at 09:09 AM.

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    That looks good, but writing a class function is a little different:
    Code:
    #include "files.h"
    
    bool files::exists(char *path[]){
    	//code
    	return false;
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    you create an "instance" of the class to use
    basicly your main function would look like this
    Code:
    #include <iostream>
    #include "files.h"
    
    int main(int argc, char *argv[])
    {
       files* MyFiles = new files;
       std::cout << std::endl;
       MyFiles->exists("your path");
       delete MyFiles;
       return 0;
    }
    or something to that effect. That is if I understood your question correctly.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    Thanks for the quick reply.

    I changed the class definition as you suggested Noir.
    Raigne, you understood correctly, but that procedure is static, so i wouldnt need to create the object (or am I missing something and things are different in c++?).

    However, I tried your code and got this error:
    main.cpp:9: error: no matching function for call to ‘files::exists(const char [10])’
    files.h:5: note: candidates are: static bool files::exists(char**)

  5. #5
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Do you really need to use a pointer? This works better:
    Code:
    #include <iostream>
    #include "files.h"
    
    
    int main( int argc, char *argv[] ) {
       std::cout << std::boolalpha << MyFiles::exists( argv + 1 ) << '\n';
    }

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why not pass the
    const char* path?
    Why do you need this pointer to array thing?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    vart, thats kinda not the point right now...

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    How is so?
    Last question was
    However, I tried your code and got this error:
    main.cpp:9: error: no matching function for call to ‘files::exists(const char [10])’
    files.h:5: note: candidates are: static bool files::exists(char**)
    exactly about this...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    Sorry, didn't know that...
    So I replace char *argv[] with const char* path?
    If so, this is what i get now: undefined reference to `files::exists(char const*)'
    Last edited by Grim; 03-28-2007 at 09:41 AM.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Did you fix it everywhere?

    I'm also not quite sure why you need classes here. If you want to decorate the names why not put related things in a namespace instead?

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    Yes, i fixed it everywhere.
    The classes are... a matter of personal preference

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The classes are... a matter of personal preference
    If those static member functions are not going to access any static member variables, then they really should be free functions instead, possibly in a namespace of your choice.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    The above code is pretty much just an example.
    I will need classes because i'll need objects, so I should really get this working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM