-
July 11th, 2006, 08:47 AM
#1
#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.
-
July 11th, 2006, 08:54 AM
#2
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
-
July 11th, 2006, 08:55 AM
#3
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.
-
July 11th, 2006, 09:02 AM
#4
Re: #include <vector> and ofstream
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.
-
July 11th, 2006, 09:11 AM
#5
Re: #include <vector> and ofstream
-
July 11th, 2006, 10:06 AM
#6
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.
-
July 11th, 2006, 11:27 AM
#7
Re: #include <vector> and ofstream
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
-
July 11th, 2006, 05:36 PM
#8
Re: #include <vector> and ofstream
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|