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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gcc compile error: conversion from 'const x[2]' to non-scalar type...

Status
Not open for further replies.

cpjust

Programmer
Joined
Sep 23, 2003
Messages
2,132
Location
US
Hi,
I'm getting an error when compiling the following code on gcc 2.95.2 (yes I know it's old, but I'm stuck with it). It compiles fine in Visual Studio though. Can anyone figure out what's going on? If I remove the const from the array definition it will compile, but I want it to be const.

Here's the error:
conversion from `const WordScoreMap[2]' to non-scalar type `WordScoreMap' requested

Thanks.

Code:
struct WordScoreMap
{
    std::string  word;
    int  score;
};

static const WordScoreMap WORDS[ 3 ][ 2 ] = {
	{ WordScoreMap( "one",	10 ), WordScoreMap( "first",	5 ) },
	{ WordScoreMap( "two",	10 ), WordScoreMap( "second",	5 ) },
	{ WordScoreMap( "three",  10 ), WordScoreMap( "third",	5 ) } };
 
Don't you need a constructor definition for WordScoreMap? Something like
Code:
   WordScoreMap (const std::string w, const int s)
   :  word(w)
   ,  score(s)
   {}
Strange that it builds at all on any compiler.
 
Oops, I forgot to include that in my post. Yes, I have a constructor like that and still get the error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top