CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2003
    Posts
    731

    #include <vector> and ofstream

    I have an ofstream object for file output which works great.
    Code:
    	ofstream fout;
    	fout.open(sFileNAme, ios::out);
    .....
    Now I added #include <vector> obviously to use vector and the moment I add this, I get compile error with ofstream object:

    error C2872: 'ofstream' : ambiguous symbol

    How to fix this error?
    Good Answers, Good Points.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: #include <vector> and ofstream

    when you included vector, did you do a "using namespace std;"

    if so, are you using :

    Code:
    #include <fstream> // correct
    
    //or
    
    #include <fstream.h>  // not correct

  3. #3
    Join Date
    Mar 2001
    Posts
    2,529

    Re: #include <vector> and ofstream

    Hmm I had no problem with it.

    Try moving #include <vector> to stdafx.h

    HTH,
    ahoodin
    To keep the plot moving, that's why.

  4. #4
    Join Date
    Jul 2003
    Posts
    731

    Re: #include <vector> and ofstream

    Quote Originally Posted by Philip Nicoletti
    when you included vector, did you do a "using namespace std;"

    if so, are you using :

    Code:
    #include <fstream> // correct
    
    //or
    
    #include <fstream.h>  // not correct
    Yes, I do "using namespace std;"

    I didn't get how do I correct the problem? I commented #include "fstream.h" but it results in even more errors:

    first in the list is:

    error C2079: 'fout' uses undefined class 'basic_ofstream<char,struct std::char_traits<char> >'
    Good Answers, Good Points.

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: #include <vector> and ofstream

    Well, you need to :

    Code:
    #include <fstream>

  6. #6
    Join Date
    Jul 2003
    Posts
    731

    Re: #include <vector> and ofstream

    Thanks that is resloved. A follow up question though on different topic.

    I want to to initialize a vector, is that possible?

    something like:
    Code:
    vector <string> names (10) = {"Mark", "Dave", "Steve", "Jack"};
    This does not compile, but can I initialize my vector some way?
    Good Answers, Good Points.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: #include <vector> and ofstream

    Quote Originally Posted by retry
    Thanks that is resloved. A follow up question though on different topic.

    I want to to initialize a vector, is that possible?

    something like:
    Code:
    vector <string> names (10) = {"Mark", "Dave", "Steve", "Jack"};
    This does not compile, but can I initialize my vector some way?
    Code:
    #include <vector>
    #include <string>
    
    void foo()
    {
       char *pNames[] = {"Mark", "Dave", "Steve", "Jack"};
       int numnames = sizeof(pNames) / sizeof(pNames[0])'
       std::vector<std::string> names(pNames, pNames + numnames);
    }
    Note that if you're using Visual C++ 6.0, this will not compile, since VC 6.0 is brain-dead when it comes to automatically converting const char* to string.

    It compiles fine using the Comeau compiler, and I suspect it will compile under Visual Studio 2003 and above.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: #include <vector> and ofstream

    Quote Originally Posted by Paul McKenzie
    Code:
    #include <vector>
    #include <string>
    
    void foo()
    {
       char *pNames[] = {"Mark", "Dave", "Steve", "Jack"};
       int numnames = sizeof(pNames) / sizeof(pNames[0])'
       std::vector<std::string> names(pNames, pNames + numnames);
    }
    Note that if you're using Visual C++ 6.0, this will not compile, since VC 6.0 is brain-dead when it comes to automatically converting const char* to string.

    It compiles fine using the Comeau compiler, and I suspect it will compile under Visual Studio 2003 and above.

    Regards,

    Paul McKenzie
    Surprised that compiles on Comeau. Should be const char * pNames[] or even const char * const pNames[].

    I have a lovely constructor in some of my classes that takes something like

    const T (&arr)[N] where N is a template parameter and T is a type (possibly templated). Had they thought about it at the time they'd have given std::vector such a constructor.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured