Re: std::map<int , std::string[2]> problems
Regarding my test program that compiles but crashes:
#include <iostream>
#include <string>
#include <map>
#include <utility>
using std::cout;
using std::endl;
using std::string;
using std::map;
using std::pair;
using std::make_pair;
int main(void)
{
string Splat[2] = {"apple", "peach"};
map<int, string[2]> fileSets;
fileSets.insert (make_pair<int, string[2]>(37, Splat));
return 0;
}
The comments went:
Gernot Frisch:[color=blue]
> The second argument must either have an = operator
> or an cctor - I'm not sure, I think it't the = op,
> which string[2] does not offer.[/color]
Mathias Waack:[color=blue]
> For each STL container holds that the value type must be
> assignable and copy constructable (23.1.3).
> But why does this code compile?[/color]
"Gernot Frisch":[color=blue]
> The code compiles fine at least with gcc 3.3.5. And raises an
> exception
> during runtime.
> How 'bout gcc 3.4?[/color]
I'm using the djgpp port of Gnu gcc version 3.4.3
in Windows 2000 Professional DOS console.
I have all the warning flags turned on, too:
-Wall -Wextra -pedantic etc etc etc
But gcc gives no warnings or errors when compiling
the above program.
I can see now that y'all are right, the program is flawed to the
core because the list elements can't be copied. I'm pretty sure
gcc creates an implicit default copy constructor for the pairs,
but the ctor is going to make ptrs that point to God knows where,
hence the GPFs.
I think the following is more like what the OP wanted, and this
compiles and runs without error:
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <utility>
using std::cout;
using std::endl;
using std::string;
using std::map;
using std::vector;
using std::pair;
using std::make_pair;
class Files
{
public:
void nextFile(string *, string*);
void open(vector<str ing>*, int);
//private: // make public for test
int getNumber(strin g); // Gets the number from the filename
//map<int, string[2]> fileSets; // NO!!!
map<int, pair<string,str ing> > fileSets; // YES!!!
map<int, pair<string,str ing> >::iterator iter;
};
int main(void)
{
Files MyFiles;
pair<string,str ing> Splat;
Splat = make_pair<strin g,string>("appl e", "peach");
MyFiles.fileSet s.insert(make_p air<int, pair<string,str ing> >(48, Splat));
Splat = make_pair<strin g,string>("cucu mber", "dill");
MyFiles.fileSet s.insert(make_p air<int, pair<string,str ing> >(23, Splat));
cout
<< "Map element 23 first string = "
<< MyFiles.fileSet s[23].first
<< endl;
cout
<< "Map element 23 second string = "
<< MyFiles.fileSet s[23].second
<< endl;
cout
<< "Map element 48 first string = "
<< MyFiles.fileSet s[48].first
<< endl;
cout
<< "Map element 48 second string = "
<< MyFiles.fileSet s[48].second
<< endl;
return 0;
}
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Regarding my test program that compiles but crashes:
#include <iostream>
#include <string>
#include <map>
#include <utility>
using std::cout;
using std::endl;
using std::string;
using std::map;
using std::pair;
using std::make_pair;
int main(void)
{
string Splat[2] = {"apple", "peach"};
map<int, string[2]> fileSets;
fileSets.insert (make_pair<int, string[2]>(37, Splat));
return 0;
}
The comments went:
Gernot Frisch:[color=blue]
> The second argument must either have an = operator
> or an cctor - I'm not sure, I think it't the = op,
> which string[2] does not offer.[/color]
Mathias Waack:[color=blue]
> For each STL container holds that the value type must be
> assignable and copy constructable (23.1.3).
> But why does this code compile?[/color]
"Gernot Frisch":[color=blue]
> The code compiles fine at least with gcc 3.3.5. And raises an
> exception
> during runtime.
> How 'bout gcc 3.4?[/color]
I'm using the djgpp port of Gnu gcc version 3.4.3
in Windows 2000 Professional DOS console.
I have all the warning flags turned on, too:
-Wall -Wextra -pedantic etc etc etc
But gcc gives no warnings or errors when compiling
the above program.
I can see now that y'all are right, the program is flawed to the
core because the list elements can't be copied. I'm pretty sure
gcc creates an implicit default copy constructor for the pairs,
but the ctor is going to make ptrs that point to God knows where,
hence the GPFs.
I think the following is more like what the OP wanted, and this
compiles and runs without error:
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <utility>
using std::cout;
using std::endl;
using std::string;
using std::map;
using std::vector;
using std::pair;
using std::make_pair;
class Files
{
public:
void nextFile(string *, string*);
void open(vector<str ing>*, int);
//private: // make public for test
int getNumber(strin g); // Gets the number from the filename
//map<int, string[2]> fileSets; // NO!!!
map<int, pair<string,str ing> > fileSets; // YES!!!
map<int, pair<string,str ing> >::iterator iter;
};
int main(void)
{
Files MyFiles;
pair<string,str ing> Splat;
Splat = make_pair<strin g,string>("appl e", "peach");
MyFiles.fileSet s.insert(make_p air<int, pair<string,str ing> >(48, Splat));
Splat = make_pair<strin g,string>("cucu mber", "dill");
MyFiles.fileSet s.insert(make_p air<int, pair<string,str ing> >(23, Splat));
cout
<< "Map element 23 first string = "
<< MyFiles.fileSet s[23].first
<< endl;
cout
<< "Map element 23 second string = "
<< MyFiles.fileSet s[23].second
<< endl;
cout
<< "Map element 48 first string = "
<< MyFiles.fileSet s[48].first
<< endl;
cout
<< "Map element 48 second string = "
<< MyFiles.fileSet s[48].second
<< endl;
return 0;
}
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Comment