error: braces around scalar initializer...

This is a discussion on error: braces around scalar initializer... within the C++ Programming forums, part of the General Programming Boards category; The following snippet encompasses lines 4-56 in my code: Code: record tmpKnowledgeBase[] = { {"REPETITION T1**"}, LINK_SPECIAL, { "Why are ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    23

    error: braces around scalar initializer...

    The following snippet encompasses lines 4-56 in my code:

    Code:
    	record tmpKnowledgeBase[] = {
    		{"REPETITION T1**"},
    		LINK_SPECIAL,
    		{
    			"Why are you repeating yourself?",
    			"I heard you the first time.",
    			"This conversation is starting to bore me.",
    			"Don't you have anything else to say?"
    		},
    		{"REPETITION T2**"},
    		LINK_SPECIAL,
    		{
    			"Yes, I know. You already said that.",
    			"Didn't you already say that?",
    			"I'm getting the impression you're repeating yourself."
    		},
    		{"BOT DONT UNDERSTAND**"},
    		LINK_SPECIAL,
    		{
    			"What are you talking about?.",
    			"I'm not sure I understand what you mean...",
    			"Huh?"
    		},
    		{"NULL INPUT**"},
    		LINK_SPECIAL,
    		{
    			"What the heck is that supposed to mean?",
    			"At least take the time to say SOMETHING.",
    			"Talking requires TWO people, you know."
    		},
    		{"NULL INPUT REPETITION**"},
    		LINK_SPECIAL,
    		{
    			"...",
    			"Wow, that's really annoying. Stop it.",
    			"What's wrong with you?",
    			"This isn't funny - it's just stupid."
    		},
    		{"HELLO", "HI"},
    		LINK_OR,
    		{
    			"Hey there!",
    			"Hello.",
    			"Hiya!"
    		},
    		{"WHAT", "YOUR", "NAME"},
    		LINK_AND,
    		{
    			"My name is "+botName+"!",
    			"Does it really matter?",
    			botName+", at your service."
    		}
    	};
    Here are the declarations for structs record and keywords:
    Code:
    typedef struct {
    	std::string keyset[TURNERBOT_MAX_KEYWORDS];
    	int linkType;
    } keywords;
    
    typedef struct {
    	keywords keys;
    	std::string response[TURNERBOT_MAX_RESP];
    } record;
    Now, this looks fine to ME, but... I'm not that great a C++. I'm getting the following error when I try to compile:
    Code:
    /home/shane/TurnerBot/src/bot.cpp: In member function 'void TurnerBot::generateKnowledgeBase()':
    /home/shane/TurnerBot/src/bot.cpp:56: error: braces around scalar initializer for type 'std::string'
    /home/shane/TurnerBot/src/bot.cpp:56: error: braces around scalar initializer for type 'int'
    /home/shane/TurnerBot/src/bot.cpp:56: error: braces around scalar initializer for type 'std::string'
    /home/shane/TurnerBot/src/bot.cpp:56: error: braces around scalar initializer for type 'std::string'
    /home/shane/TurnerBot/src/bot.cpp:56: error: braces around scalar initializer for type 'int'
    /home/shane/TurnerBot/src/bot.cpp:56: error: braces around scalar initializer for type 'std::string'
    /home/shane/TurnerBot/src/bot.cpp:56: error: braces around scalar initializer for type 'std::string'
    /home/shane/TurnerBot/src/bot.cpp:56: error: braces around scalar initializer for type 'int'
    /home/shane/TurnerBot/src/bot.cpp:56: error: braces around scalar initializer for type 'std::string'
    /home/shane/TurnerBot/src/bot.cpp:56: error: braces around scalar initializer for type 'std::string'
    Can somebody please tell me what this is talking about and how I can fix it? =/

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I think I ran into this once before. Although a single string literal initializer may be optionally enclosed in braces...
    Code:
    const char text[] = {"initializer"};
    ...I don't believe this recurses to members of an aggregate. I believe the fix is to write them as
    Code:
    const char text[] = "initializer";
    [edit]Looks up and sees it's C++.
    Either that or there's using the string constructor for these values.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Your brace placement is wrong. The way you have it, the compiler thinks that {"REPETITION T1**"} is the initializer for a complete record struct.

    Should be
    Code:
    record tmpKnowledgeBase[] = {
    	{
    		{
    			{"REPETITION T1**"},
    			LINK_SPECIAL
    		},
    		{
    			"Why are you repeating yourself?",
    			"I heard you the first time.",
    			"This conversation is starting to bore me.",
    			"Don't you have anything else to say?"
    		}
    	},
    // ...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:14 PM
  2. Replies: 12
    Last Post: 01-17-2009, 03:35 AM
  3. two dimensional int array access problems ;/
    By krST in forum C Programming
    Replies: 6
    Last Post: 01-04-2006, 06:02 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. initializer?
    By Luigi in forum C++ Programming
    Replies: 0
    Last Post: 11-29-2002, 11:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21