Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Initialisation of struct containing string?

Status
Not open for further replies.

hughLg

Programmer
Feb 18, 2002
136
MY
Can't initialize the variable a, why:

Code:
#include <iostream>
using namespace std;

typedef struct {
	string a, b;
	int c;
} T;

int main() {
	[highlight]T a = {"a", "b", 5};[/highlight]
}

The following error & warning messages appear:

[highlight]Error E2291 test.cpp 11: } expected in function main()
Error E2034 test.cpp 11: Cannot convert 'char *' to 'T' in function main()
Error E2141 test.cpp 11: Declaration syntax error in function main()
Error E2139 test.cpp 11: Declaration missing ; in function main()
Warning W8004 test.cpp 11: 'a' is assigned a value that is never used in function main()
Error E2190 test.cpp 11: Unexpected }
Error E2190 test.cpp 12: Unexpected }[/highlight]
 
You can't initialize a struct like that.

You'll have to either give the struct a constructor (in which case you may as well make it a class instead), or initialize a, b, and c individually.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top