Can't compile source code with string [SOLVED]

  • On 05/09/2014 at 14:09, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   14 
    Platform:   Windows  ;   
    Language(s) :     C++  ;

    I have, in my code, variables of type String and variables of type string.
    I'm using some std:: stuff so I must use string instead of String.
    It compiles and works fine on my Mac.
    But while trying to compile it in Windows, I get lots of errors and the compilation fails.
    Is there anything that can be done?

  • On 05/09/2014 at 16:49, xxxxxxxx wrote:

    Why does it compile without a single complain in Xcode and, in Visual Studio I get dozens of errors?!?

  • On 05/09/2014 at 18:39, xxxxxxxx wrote:

    Are you making string explicit to the std library by using std::string for references?

  • On 05/09/2014 at 18:46, xxxxxxxx wrote:

    I don't know what you mean.
    Can you give me an example?

  • On 05/09/2014 at 18:52, xxxxxxxx wrote:

    std is a namespace.  You can declare the namespace for referencing objects in that space explicitly (std::space) or implicitly (using namespace std) somewhere at the top of the file.  Which to use will depend on if there are possible clashes.  Let's say you have a string type in std and another in another namespace included in your code.  If they are not explicitly declared, there is a chance that the compiler doesn't understand which one to use.  If you are just using 'string', you must add 'using namespace std'.

  • On 06/09/2014 at 14:54, xxxxxxxx wrote:

    Thank you so much, Robert.
    What was happening was the I had these two lines:

    template <typename T> string tostr(const T& t) { ostringstream os; os<<t; return os.str(); }   
    using namespace std;   

    and I simply had to switch them, like this:

    using namespace std;   
    template <typename T> string tostr(const T& t) { ostringstream os; os<<t; return os.str(); }   

    The correct way is the second, of course. But it is weird that Xcode doesn't complain and makes it work, somehow.

  • On 06/09/2014 at 15:59, xxxxxxxx wrote:

    Technically, you can declare 'using namespace' anywhere in the global scope of the source code.  You can do this with '#include" for headers as well.  It is generally safer to do these first nonetheless.  And you want to declare the namespace after any header that defines it (!).

  • On 07/09/2014 at 09:29, xxxxxxxx wrote:

    All what Kuroyume wrote. However, I would encourage you to not use "using namespace x" but instead use x::function. This avoids any library conflicts.

  • On 07/09/2014 at 12:28, xxxxxxxx wrote:

    Agreed! 🙂

  • On 07/09/2014 at 12:56, xxxxxxxx wrote:

    Thank you all. I will take that into account in the future.
    In the meanwhile, I could make it all work now.

Log in to reply