0

For the code below, I get the following messages. These are:

1>c:\users\s1\desktop\c++folder\pr5\pr5\pr5.cpp(11): error C2078: too many initializers
1>c:\users\s1\desktop\c++folder\pr5\pr5\pr5.cpp(13): error C2143: syntax error : missing ';' before '.'
1>c:\users\s1\desktop\c++folder\pr5\pr5\pr5.cpp(13): error C2373: 'newBean' : redefinition; different type modifiers
1>c:\users\s1\desktop\c++folder\pr5\pr5\pr5.cpp(12) : see declaration of 'newBean'
1>c:\users\s1\desktop\c++folder\pr5\pr5\pr5.cpp(14): error C2143: syntax error : missing ';' before '.'

This is the code below. How can I please fix the code ? I have made the struct members to be static const.

#include <iostream>
#include <string>
using namespace std;

 struct coffeeBean
{
    static const string name;
    static const string country;
    static const int strength;
};
 coffeeBean myBean = {"yes", "hello", 10 };
 coffeeBean newBean;
 const string newBean.name = "Flora";
 const string newBean.country = "Mexico";
 const int newBean.strength = 9; 

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

 cout << "Coffee bean " + newBean.name + " is from " + newBean.country << endl;
 system("pause");
 return 0;
}
1
  • 2
    You are confused about something. static means that only one, global instance of that member exists for all objects. Therefore each coffeeBean cannot have its own name. Commented Nov 23, 2015 at 0:03

2 Answers 2

3
#include <iostream>
#include <string>
using namespace std;

struct coffeeBean
{
    string name;                     
    string country;                         
    int strength;
};
 coffeeBean myBean = {"yes", "hello", 10 };
 coffeeBean newBean;

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

newBean.name = "Flora";
newBean.country = "Mexico";
newBean.strength = 9; 
 cout << "Coffee bean " + newBean.name + " is from " + newBean.country << endl;
 system("pause");
 return 0;
}

Couple of things:

If you want to initialize a variable, do not do it in global scope.

If you want to assign to variable, do not declare a type on it:

const string newBean.name = "Flora";//declare new variable, or assign to newBean.name ??

Just assign to it like this:

newBean.name = "Flora";

Use static, if you want to have a variable, that is common for all instances of classes. If you want a variable, that is different across instances (common use of OOP), do not declare const.

And lastly, declare constants, if you do not plan on changing value.

Sign up to request clarification or add additional context in comments.

2 Comments

I guess u could also place coffeeBean myBean = {"yes", "hello", 10 }; coffeeBean newBean; inside main().
@solti It would be better, but it's legal and I would not want to change more, than is needed, to not confuse correctness with style.
-1
#include <iostream>
#include <string>
using namespace std;

struct coffeeBean
{
    string name; // can't be static because you want more 
                 // than one coffeeBean to have different values
    string country; // can't be const either because newBean 
                    // will default-construct and then assign to the members
    int strength;
};
 coffeeBean myBean = {"yes", "hello", 10 };
 coffeeBean newBean;
 newBean.name = "Flora";
 newBean.country = "Mexico";
 newBean.strength = 9; 

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

 cout << "Coffee bean " + newBean.name + " is from " + newBean.country << endl;
 system("pause");
 return 0;
}

fixed. see comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.