i am trying to declare a char[][] like this:
Granery::foodNames[5][100] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};
while in the header i have: - #ifndef Granery_h
-
#define Granery_h
-
class Granery
-
{
-
public:
-
Granery();
-
static char foodNames[5][100];
-
};
-
#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
15 8935
Why are you using character arrays in C++? That's what std::string was made for.
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
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: - .....
-
const string foodnames[5]
-
.....
-
const string Granery::foodNames[5] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};
ok would this be correct... - const string Granery::foodNames = {"Meat","Fruit","Wheat","Vegetables","Dairy"};
with: - 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
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: -
//you iinclude the header
-
class Granery
-
{
-
public:
-
Granery();
-
static char foodNames[5][100];
-
};
-
//in the source file
-
char Granery::foodNames[5][100] = "Meat","Fruit","Wheat","Vegetables","Dairy"};
-
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
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.
actually i updated it in the granery header too... sorry i failed to mention that
static string foodNames[5];
thats in the header
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.
ok sorry... here is the granery class right now: - #include "Granery.h"
-
#include <string>
-
using namespace std;
-
-
Granery::Granery()
-
{
-
string Granery::foodNames[5] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};
-
}
and here is the granery class header: - #include <string>
-
using namespace std;
-
-
#ifndef Granery_h
-
#define Granery_h
-
class Granery
-
{
-
public:
-
Granery();
-
static string foodNames[5];
-
};
-
#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
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.
i feel like my computer just hates me...
now i fized that so i have: - #include "Granery.h"
-
#include <string>
-
using namespace std;
-
-
string Granery::foodNames[5] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};
-
-
Granery::Granery()
-
{
-
-
}
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
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. -
class Granery
-
{
-
public:
-
Granery();
-
static string foodNames[5];
-
};
-
string Granery::foodNames[5] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};
-
-
Granery::Granery()
-
{
-
-
}
-
int main()
-
{
-
-
}
-
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ying Yang |
last post by:
Hi,
whats the difference between:
char* a = new char;
char* b = new char;
char c ;
|
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...
|
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;
|
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...
|
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...
|
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...
|
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
|
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...
|
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 )
{
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |