Thread: function in other file

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    90

    function in other file

    Hello,
    I have this function
    Code:
    #include <io.h>
    #include <string>
    #include <fstream>
    
    int rec_dir_list(string path){
      static int count = 0;
      struct _finddata_t c_file;
      long fh;
    
      string t_path = path;
      t_path += "\\*.*";
    
      if((fh=_findfirst(t_path.c_str(),&c_file)) != -1)
        while(_findnext(fh, &c_file) == 0)
          // ignore '.' and '..'
          if(strncmp(c_file.name, ".", 1) != 0
             && strncmp(c_file.name, "..", 2) != 0) {
            if((c_file.attrib & _A_SUBDIR) == _A_SUBDIR) {
              rec_dir_list(path + "\\" + c_file.name);
              cout << "DIR: " << path << "\\" << c_file.name << endl;
              count++;
            }
          }
      return count;
    }
    which is called from main file, function main like this

    Code:
     string DIR="c:";
        cout << "There are " << rec_dir_list(DIR) << " sub directories\n";
    and I get errors during compiling:
    C:\Users\John\Desktop\aass\bClass.cpp|5|error: 'string' was not declared in this scope|
    C:\Users\John\Desktop\aass\bClass.cpp|5|error: expected ',' or ';' before '{' token|
    ||=== Build finished: 2 errors, 0 warnings ===|

    But If I use same function placed in (single) main file it works OK.

    What do I do wrong?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    string is part of the std namespace. You have to refer to it as std::string or add a using declaration or using directive.

    I'm guessing it works in your main file because you have the using namespace std there.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    Yes Daved,
    thank you wery much. Your guessing is correct.
    I added
    Code:
    using namespace std;
    A have also change <string> to <string.h> because of strncmp function.
    Is this OK?

    ...and this error gone
    But, now I have one new in main file
    C:\Users\John\Desktop\aass\Main.cpp||In function 'int main()':|
    C:\Users\John\Desktop\aass\Main.cpp|70|error: 'rec_dir_list' was not declared in this scope|
    ||=== Build finished: 1 errors, 0 warnings ===|

    How to declare function which is in another file?
    I try with header file like this:
    Code:
    rec_dir_list(string DIR);
    But compiler says "string not declared..."

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> A have also change <string> to <string.h> because of strncmp function

    I would not do that. The first reason is that they are two different files. If you use std::string you need <string> and if you use strncmp you need either <string.h> or <cstring>. The second reason is that you don't really need strncmp. It's a C version of comparing. You can use string class functions. For now #include both <string> and <string.h>, but I'd consider looking into how to do those compares with string functions instead so you can get rid of <string.h>.

    >> How to declare function which is in another file?

    This is the same problem as before. Again, using std::string or putting using namespace std in that header will fix it. Putting the using directive in a header file is generally considered a bad idea, though. So I would just use std::string for the function declaration in the header.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    Well, I notice that for now I have only bad ideas
    Because still starting to learn c++.
    Function above was copyed from somewhere and I would change them with advancing...

    In single file it works (I get dir listing in console window) so I take it because there's not much things which works for me now

    So, I added both string and string.h what is luckyly allowed.
    And still one error remains...

    C:\Users\John\Desktop\aass\bClass.h|11|error: expected constructor, destructor, or type conversion before ';' token|
    ||=== Build finished: 1 errors, 0 warnings ===|

    bClass.h
    Code:
    #include <string>
    // here is no need for string.h
    
    class MyClass //no matter here
    {
    public:
        int a;
        char Name[40];
        float b;
    };
    
    // here is error
    rec_dir_list(std::string DIR);

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    At first glance the only thing I see is that you forgot to put the 'int' return type in front of the function.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You really should make Name of type std::string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    Daved,
    You are real guru!
    Thank you wery much.
    My test program now works fine.
    I have VB background and here things are much different in most cases so I don't "see" yet such obvious things. Matter of learning and training.

    Elysia, second step will be passing structure data readed from file through functions.
    Then I will see what to pass. String, char or pointer, regarding of speed.
    Anyway, thanks for advice.

  9. #9
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Just use std::string. If you're too concerned about speed to use std::string, then you're too concerned with speed to use C++, and C would fit your needs much better. What I mean is the difference between using C and using C++ is 100 times more significant than the difference between std::string and char*. Not that I'm saying switch to C, I mean that you should make your life easier and use the classes provided. std::string is internally just a char[], it just provides an easy to use interface.

    String, char or pointer, regarding of speed.

    The fact that you list char and pointer shows a misunderstanding. You can't pass a string(string in the sense of a group of chars, not in the sense of the class std::string) as chars, you must pass a pointer to an array of chars.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    User Name, I cant decide now what to use C or C++, or where to use which becuse my missunderstandings is obvious. Not just strings, but oop, project organisation, libraryes etc, etc...
    But I have real data and working examples and good understanding of thema in VB.
    Now I decide to start learning C/C++ in practical examples with my data at way which is wery easy for me to do with VB. So, anything what work is good for me now but with time I will change things with better (more suitable) solutions.
    Unfortunately, this require a time.

    Thanks for all which helped.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    "Premature optimization is the root of much evil."
    That quote comes who someone who knows what he's saying.
    Don't be concerned about the speed. Use the tools at your hand.
    If the program happens to be too slow one day, then you use a profiler and find the bottleneck.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Quote Originally Posted by nime View Post
    So, anything what work is good for me now but with time I will change things with better (more suitable) solutions.
    Unfortunately, this require a time.
    Why learn bad habits, only have to spend time breaking them later? The people here, who know enough about both the languages to know which practices are better, are trying to guide you so you don't have waste your time to reinventing the wheel. For now, just listen, reasons become clear with experience.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM