Thread: -,- Deference a scope?

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    -,- Deference a scope?

    Well, looks like mingw isint as happy about scopes as borland was.

    Well, its hard to explain so:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string.h>
    #include "Definitions.h"
    using namespace std;
    
    int LoadScript(string path) {
     ifstream file_in(path.c_str(), ios::nocreate); //Mingw assumes I want std::ios::nocreate.
    
     if(!file_in) {
      return ERROR_Load_Failed;
     }
    
     cout << file_in;
    }
    So, is there some way to termporarialy dereference std so I can use ios?
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do not use a using namespace directive. Still, a solution for your problem without doing that is to specify the global namespace, e.g.,
    Code:
    #include <iostream>
    #include <fstream>
    #include <string.h>
    #include "Definitions.h"
    using namespace std;
    
    int LoadScript(string path) {
     ifstream file_in(path.c_str(), ::ios::nocreate); //Mingw assumes I want std::ios::nocreate.
    
     if(!file_in) {
      return ERROR_Load_Failed;
     }
    
     cout << file_in;
    }
    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

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Hmm, your example didint work. Mingw still assumes I want std::ios instead of just ios::

    Code:
     ifstream file_in(path.c_str(), ::ios::nocreate);
    When I dont use std, it gets very ugly very quickly. When the file gets bigger it'll be std:: all over. I'd rather not have to do that just over one thing -,-.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hmm, your example didint work. Mingw still assumes I want std::ios instead of just ios::
    That's strange. Could you provide an example that I can test with?

    When I dont use std, it gets very ugly very quickly. When the file gets bigger it'll be std:: all over. I'd rather not have to do that just over one thing -,-.
    Use specific using directives. If it is a header file, you should fully qualify the names, even if you find it inconvenient.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string.h>
    #include "Definitions.h"
    
    using std::ifstream;
    using std::cout;
    
    int LoadScript(string path) {
     ifstream file_in(path.c_str(), ios::nocreate);
    
     if(!file_in) {
      return ERROR_Load_Failed;
     }
    
     cout << file_in;
    }
    Oh, and your function should return an int. There is a code path where it returns void.
    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

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Oh, I didint realize I could specify which headers/functions used a namespace.

    That's strange. Could you provide an example that I can test with?
    I used basicaly the same code you posted above.

    When I specify everything using the proper namespaces, it just says ios is undeclared. I'll just stop being picky and assume the file I'm opening exists >.>.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I used basicaly the same code you posted above.
    Unfortunately, only you know what is inside "Definitions.h"

    When I specify everything using the proper namespaces, it just says ios is undeclared. I'll just stop being picky and assume the file I'm opening exists
    That's because ios is under the std namespace, unless you have your own ios in Definitions.h, which I assumed was the case. Incidentally, std::ios::nocreate does not exist, as far as I know.
    Last edited by laserlight; 08-27-2006 at 03:20 AM.
    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

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    AFAIK ios::nocreate is from <fstream.h>. It is not standard and not part of <fstream>. I don't think you even need it.

    Also, it should be <string>, not <string.h>. They are different headers, and since you are using the string class, you want <string>.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Blackroot
    When I dont use std, it gets very ugly very quickly. When the file gets bigger it'll be std:: all over. I'd rather not have to do that just over one thing -,-.
    Using std:: all over is the preferred way of doing things anyway.

    Personally I think they never should have added "using", it just makes for sloppier code, but that's me.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    The other alternative, if possible, is to wrap everything from Definitions.h inside its own namespace, and use fully qualified names for that header file. One of the problems namespaces are supposed to solve is name clashes like the one you're experiencing. This is a good example of why using namespace std; is generally not reccomended outside of toy programs.
    Last edited by Bench82; 08-27-2006 at 05:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  5. Question about C# scope rules
    By converge in forum C# Programming
    Replies: 3
    Last Post: 01-30-2002, 06:56 AM