473,394 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

char arrays as class member variables

112 100+
i am trying to declare a char[][] like this:

Granery::foodNames[5][100] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};

while in the header i have:


Expand|Select|Wrap|Line Numbers
  1.       #ifndef Granery_h
  2.       #define Granery_h
  3.       class Granery
  4.       {
  5.         public:
  6.          Granery();
  7.          static char foodNames[5][100];
  8.       };
  9.       #endif


when i compile this i get an error :

[C++ Error] Granery.cpp(5): E2188 Expression syntax

that error is on the line in the .cpp code
Granery::foodNames[5][100] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};

i have also tried:
Granery::foodNames = {"Meat","Fruit","Wheat","Vegetables","Dairy"};

any help with how i shud be initializing the char[][]?

thanks,
ken
Oct 15 '07 #1
15 8935
Ganon11
3,652 Expert 2GB
Why are you using character arrays in C++? That's what std::string was made for.
Oct 15 '07 #2
drsmooth
112 100+
ok..good point but i still have the same problem with the string[]

Granery::foodNames = {"Meat","Fruit","Wheat","Vegetables","Dairy"};

declared in the Granery.cpp default contructor gives me this:

[C++ Error] Granery.cpp(7): E2188 Expression syntax


i realize this may sound like a dumb question, but for some reason im having a hard time with this


thanks again
ken
Oct 15 '07 #3
RRick
463 Expert 256MB
Your basic problem is that when you try to initialize the static variable, you are not declaring it correctly. When inititializing a static variable outside the class, you have to include the type. In your case, you need to add char before the name. You also need to make the char constant since you're initializing the array to constant strings.

For strings you woud write:
Expand|Select|Wrap|Line Numbers
  1.  .....
  2. const string foodnames[5]
  3. .....
  4.  const string Granery::foodNames[5] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};
Oct 16 '07 #4
drsmooth
112 100+
ok would this be correct...

Expand|Select|Wrap|Line Numbers
  1. const string Granery::foodNames = {"Meat","Fruit","Wheat","Vegetables","Dairy"};
with:
Expand|Select|Wrap|Line Numbers
  1. const static string foodNames[5];
in the header?

i set it up like that and got this error:
[C++ Error] Granery.cpp(7): E2089 Identifier 'foodNames' cannot have a type qualifier


thanks alot
ken
Oct 16 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
What is the problem?

You declare a static variable in a class (this does not define it) and then you define it outside the class in the source file:
Expand|Select|Wrap|Line Numbers
  1. //you iinclude the header
  2. class Granery
  3.       {
  4.         public:
  5.          Granery();
  6.          static char foodNames[5][100];
  7.       };
  8. //in the source file
  9. char Granery::foodNames[5][100] = "Meat","Fruit","Wheat","Vegetables","Dairy"};
  10.  
Oct 16 '07 #6
drsmooth
112 100+
my problem is when i put this line in my source:

string Granery::foodNames[5] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};

i get this error:

[C++ Error] Granery.cpp(7): E2089 Identifier 'foodNames' cannot have a type qualifier


i feel like i must be missing something because what you're saying makes sense, its just not working...or at least i think so
Oct 16 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
string Granery::foodNames[5] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};

i get this error:

[C++ Error] Granery.cpp(7): E2089 Identifier 'foodNames' cannot have a type qualifier


i feel like i must be missing something because what you're saying makes sense, its just not working...or at least i think so
Yes you are.

The array you are creating is an array of 5 strings. The array in the Granery class is a char [5][100] array.

Look at my post #6.
Oct 17 '07 #8
drsmooth
112 100+
actually i updated it in the granery header too... sorry i failed to mention that

static string foodNames[5];

thats in the header
Oct 17 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
actually i updated it in the granery header too... sorry i failed to mention that

static string foodNames[5];

thats in the header
Maybe you should repost all of the code so I can see what is currently there. That is, if you still have a problem.
Oct 18 '07 #10
drsmooth
112 100+
ok sorry... here is the granery class right now:

Expand|Select|Wrap|Line Numbers
  1. #include "Granery.h"
  2. #include <string>
  3. using namespace std;
  4.  
  5. Granery::Granery()
  6. {
  7.  string Granery::foodNames[5] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};
  8. }
and here is the granery class header:

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. using namespace std;
  3.  
  4. #ifndef Granery_h
  5. #define Granery_h
  6. class Granery
  7. {
  8.   public:
  9.    Granery();
  10.    static string foodNames[5];
  11. };
  12. #endif
the error i get is on the code at line 7 in the granery class...the error is :

[C++ Error] Granery.cpp(7): E2089 Identifier 'foodNames' cannot have a type qualifier
Oct 18 '07 #11
Ganon11
3,652 Expert 2GB
You are trying to assign foodNames a value inside the constructor - but the constructor is called whenever an object of type Granery is made. foodNames should only be given a value once. You should give it a value outside of the constructor, as shown before.
Oct 18 '07 #12
drsmooth
112 100+
i feel like my computer just hates me...

now i fized that so i have:

Expand|Select|Wrap|Line Numbers
  1. #include "Granery.h"
  2. #include <string>
  3. using namespace std;
  4.  
  5. string Granery::foodNames[5] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};
  6.  
  7. Granery::Granery()
  8. {
  9.  
  10. }
in my cpp code...now i get a compiler error:
[C++ Fatal Error] _string.c(565): F1004 Internal compiler error at 0xc5d0f9 with base 0xc10000

and the __string.c code pops up...


it just doesnt end...lol

thanks again
ken
Oct 18 '07 #13
weaknessforcats
9,208 Expert Mod 8TB
in my cpp code...now i get a compiler error:
[C++ Fatal Error] _string.c(565): F1004 Internal compiler error at 0xc5d0f9 with base 0xc10000

and the __string.c code pops up...


it just doesnt end...lol
Yeah. Especially since the code below compiles and links without error using Visual Studio.NET 2005. I just copied and pasted the code you posted.

Expand|Select|Wrap|Line Numbers
  1. class Granery
  2. {
  3.   public:
  4.    Granery();
  5.    static string foodNames[5];
  6. };
  7. string Granery::foodNames[5] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};
  8.  
  9. Granery::Granery()
  10. {
  11.  
  12. }
  13. int main()
  14. {
  15.  
  16. }
  17.  
Oct 19 '07 #14
drsmooth
112 100+
so i should just totally trash my compiler? lol i guess ill take it up with Borland or whoever makes this compiler...


thanks alot for your help, at least i know it should work
Oct 19 '07 #15
RRick
463 Expert 256MB
Check and see how old your Borland compiler is. If its pretty old (2+ years) then your problem might be with the STL library and the compiler. Older compilers have had no end of troubles with the STL.

Take a look at the Sgi STL web site. They might have a version that works with your compiler.
Oct 20 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

35
by: Ying Yang | last post by:
Hi, whats the difference between: char* a = new char; char* b = new char; char c ;
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
28
by: Merrill & Michele | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void){ char *p; p=malloc(4); strcpy(p, "tja"); printf("%s\n", p); free(p); return 0;
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
13
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
0
by: Adamn | last post by:
Hi everybody! I'm using Visual Studio 2005 and don't know how to solve this problem. Till now, everything worked just fine, included all the necessary libs, headers and so on. But there is the...
11
by: Dennis Jones | last post by:
Hi all, 1) Let's say you have two char 's of the same size. How would you write a no-fail swap method for them? For example: class Test { char s; void swap( Test &rhs ) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.