Code compiling in Visual Studio but not g++

I have the following program that seems to compile and run fine in Visual Studio, but I can't compile it with g++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
#include <vector>

using namespace std;

struct atom {
	string atom_type;
	int atom_number, nbonds, IDa, IDb, IDc, IDd;
	double x1, y1, z1, Xa, Ya, Za, Xb, Yb, Zb, Xc, Yc, Zc, Xd, Yd, Zd;
};

int main(int argc, char* argv[]) {

	if (2 != argc) {
		cerr << "USAGE: bam file.input";
		return 1;
	}

	string configLine; // string buffer for reading in the config file
	bool eOutput, errOutput, mOutput, bOutput, vOutput, nnOutput; // Options for choosing which output files to generate
	bool switchCode, relaxVolume;
	string atomTypeOne, atomTypeTwo, atomFixed, atomToSwitchOne, atomToSwitchTwo; // strings for atoms, could be 2 char arrays
	int atomQuantityOne, atomQuantityTwo, numberSwitches, relaxTime;
	double bondSwitchProbOne, bondSwitchProbTwo, atomSwitchProb, temp, latticeConsts[3];
	string basename; // name of output files
	ifstream configFile(argv[1]);

	string key, value, buffer;
	map<string, string> configMap;
	vector<atom> configAtoms;
	configAtoms.reserve(100);
	while (getline(configFile, buffer)) {
		if ('#' == buffer[0])
			continue;
		else if (!buffer.find('=')) {
			atom* n = new atom;
			configFile >> n->atom_type >> n->atom_number >> n->x1 >> n->y1 >> n->z1 >> n->nbonds >> n->IDa >> n->Xa >> n->Ya >> \
n->Za >> n->IDb >> n->Xb >> n->Yb >> n->Zb >> n->IDc >> n->Xc >> n->Yc >> n->Zc >> n->IDd >> n->Xd >> n->Yd >> n->Zd;
			configAtoms.push_back(*n);
		}
		else {
			key = buffer.substr(0, buffer.find('='));
			value = buffer.erase(0, buffer.find('=') + 1);
			configMap[key] = value;
		}
	}

	istringstream(configMap["output_file_prefix"]) >> basename;
	istringstream(configMap["energy"]) >> boolalpha >> eOutput;
	istringstream(configMap["error"]) >> boolalpha >> errOutput;
	istringstream(configMap["output"]) >> boolalpha >> mOutput;
	istringstream(configMap["bond"]) >> boolalpha >> bOutput;
	istringstream(configMap["vasp"]) >> boolalpha >> vOutput;
	istringstream(configMap["nn"]) >> boolalpha >> nnOutput;
	istringstream(configMap["atom_type_1"]) >> atomTypeOne;
	istringstream(configMap["atom_type_2"]) >> atomTypeTwo;
	istringstream(configMap["atom_number_1"]) >> atomQuantityOne;
	istringstream(configMap["atom_number_2"]) >> atomQuantityTwo;
	istringstream(configMap["bond_switch_prob1"]) >> bondSwitchProbOne;
	istringstream(configMap["bond_switch_prob2"]) >> bondSwitchProbTwo;
	istringstream(configMap["atom_switch_code"]) >> boolalpha >> switchCode;
	istringstream(configMap["atoms_to_switch"]) >> atomToSwitchOne >> atomToSwitchTwo;
	istringstream(configMap["atoms_switch_prob"]) >> atomSwitchProb;
	istringstream(configMap["temperature"]) >> temp;
	istringstream(configMap["number_switches"]) >> numberSwitches;
	istringstream(configMap["lattice_constants"]) >> latticeConsts[0] >> latticeConsts[1] >> latticeConsts[2];
	istringstream(configMap["relax_volume"]) >> boolalpha >> relaxVolume;
	istringstream(configMap["volume_relax_time"]) >> relaxTime;
	istringstream(configMap["atoms_fixed"]) >> atomFixed;


	cout << "basename is " << basename << endl;
	cout << "eOutput is " << eOutput << endl;
	cout << "errOutput is " << errOutput << endl;
	cout << "mOutput is " << mOutput << endl;
	cout << "bOutput is " << bOutput << endl;
	cout << "vOutput is " << vOutput << endl;
	cout << "nnOutput is " << nnOutput << endl;
	cout << "atomTypeOne is " << atomTypeOne << endl;
	cout << "atomTypeTwo is " << atomTypeTwo << endl;
	cout << "atomQuantityOne is " << atomQuantityOne << endl;
	cout << "atomQuantityTwo is " << atomQuantityTwo << endl;
	cout << "bondSwitchProbOne is " << bondSwitchProbOne << endl;
	cout << "bondSwitchProbTwo is " << bondSwitchProbTwo << endl;
	cout << "switchCode is " << switchCode << endl;
	cout << "atomToSwitchOne and Two are " << atomToSwitchOne << " " << atomToSwitchTwo << endl;
	cout << "atomSwitchProb is " << atomSwitchProb << endl;
	cout << "temp is " << temp << endl;
	cout << "numberSwitches is " << numberSwitches << endl;
	cout << "latticeConsts are " << latticeConsts[0] << " " << latticeConsts[1] << " " << latticeConsts[2] << endl;
	cout << "relaxVolume is " << relaxVolume << endl;
	cout << "relaxTime is " << relaxTime << endl;
	cout << "atomFixed is " << atomFixed << endl;

	getchar();

	return 0;
}


Some googling has led me to believe that I could be using Visual C++, which is specific to a Visual Studio environment. The main problem seems to be with the istringstream objects, but those are seemingly not specific to Visual C++. This is the length of errors I get when trying to compile:

http://dpaste.com/1A9QR37.txt
How are you trying to compile the code with g++? What version of g++ are you using? What operating system and version are you using?

The code you supplied compiles without errors for me on Linux using g++ 6.1.0 using the C++14 standard.

You need to enable C++11 in your compiler by adding -std=c++11
What are you trying to do here?
 
istringstream(configMap["output_file_prefix"]) >> basename
Are you trying to do this?
 
basename = configMap["output_file_prefix"]
1
2
istringstream(configMap["output_file_prefix"]) >> basename;
std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)

the extraction/insertion operator modify the stream, so it ask for a reference (see the &). There you are creating a termporary, and you can't bind a reference to a temporary.


However, It compiles fine in the c++11 standard. Use the -std=c++11 flag.
By the way
39
40
41
42
43
44
{
			atom* n = new atom;
			configFile >> n->atom_type >> n->atom_number >> n->x1 >> n->y1 >> n->z1 >> n->nbonds >> n->IDa >> n->Xa >> n->Ya >> \
n->Za >> n->IDb >> n->Xb >> n->Yb >> n->Zb >> n->IDc >> n->Xc >> n->Yc >> n->Zc >> n->IDd >> n->Xd >> n->Yd >> n->Zd;
			configAtoms.push_back(*n);
}
you've got a leak there
http://www.cplusplus.com/forum/general/138037/#msg731921
Also consider updating your compiler. g++4.9 is pretty old now. g++6.3.1 is the current version.
Topic archived. No new replies allowed.