Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Const Multi-Dimentional Array Member Variable

Reply
Thread Tools

Const Multi-Dimentional Array Member Variable

 
 
NvrBst
Guest
Posts: n/a
 
      11-12-2008
I'd like to do something like this but having a problem with the
proper syntax in the constructor, maybe someone knows the correct
syntax?

---MyClass.h---
#ifndef MYCLASS_H_
#define MYCLASS_H_

class MyClass {
public:
MyClass();
virtual ~MyClass();
private:
const char* const m_navi[1][1];
};

#endif /*MYMENU_H_*/
----------

---MyClass.cpp---
#include "MyClass.h"

MyClass::Myclass() {
m_navi = { {"1"} };
}

MyClass:~MyClass() {
}
----------


1. I get an "expected ';' before '{' token" error at the "m_navi = {"
line. I also get an "uninitalized member "Myclass::t" error.
2. If I change "m_navi = {" to "MyClass::m_navi = {" I get the same
errors as "1.".
3. I've tried declaring it in the header file directly (const char*
const m_navi[1][1] = { {"1"} } and get an "a brace-enclosed
initializer is not allowed here before '{' token" error.
4. I've tried making it static (static const char* const m_navi[1][1]
= { {"1"} } and get the same errors as "3."
5. If I move the (const char* const m_navi[1][1] = { {"1"} } into
the "main.cpp" file (above the "int main() {" line) then it compiles
fine and I can use it fine in the main function.

Question: Whats the proper way to assign the (const char* const m_navi
[1][1]) inside the constructor? Assuming the above "MyClass.h" and
"MyClass.cpp" files? Thanks

Note: The member variable can be static if that makes it easier; the
data inside "m_navi" never changes once it is set, and there is only
one instance of "MyClass" ever created; I would like to set the value
inside the constructor though if possible since values in "m_navi"
potentially gets updated between builds, so I'd only have to change
the ".cpp" file, but this isn't 100% nessisary.

I'm using Eclipse 3.2.2, on a Ubuntu 8.10 Machine. g++ Version
"4.2.4". Thanks.
 
Reply With Quote
 
 
 
 
Adem
Guest
Posts: n/a
 
      11-13-2008
"NvrBst" wrote:
>
> I'd like to do something like this but having a problem with the
> proper syntax in the constructor, maybe someone knows the correct
> syntax?
>
> ---MyClass.h---
> #ifndef MYCLASS_H_
> #define MYCLASS_H_
>
> class MyClass {
> public:
> MyClass();
> virtual ~MyClass();
> private:
> const char* const m_navi[1][1];
> };
>
> #endif /*MYMENU_H_*/
> ----------
>
> ---MyClass.cpp---
> #include "MyClass.h"
>
> MyClass::Myclass() {
> m_navi = { {"1"} };
> }
>
> MyClass:~MyClass() {
> }
> ----------
>
>
> 1. I get an "expected ';' before '{' token" error at the "m_navi = {"
> line. I also get an "uninitalized member "Myclass::t" error.
> 2. If I change "m_navi = {" to "MyClass::m_navi = {" I get the same
> errors as "1.".
> 3. I've tried declaring it in the header file directly (const char*
> const m_navi[1][1] = { {"1"} } and get an "a brace-enclosed
> initializer is not allowed here before '{' token" error.
> 4. I've tried making it static (static const char* const m_navi[1][1]
> = { {"1"} } and get the same errors as "3."
> 5. If I move the (const char* const m_navi[1][1] = { {"1"} } into
> the "main.cpp" file (above the "int main() {" line) then it compiles
> fine and I can use it fine in the main function.
>
> Question: Whats the proper way to assign the (const char* const m_navi
> [1][1]) inside the constructor? Assuming the above "MyClass.h" and
> "MyClass.cpp" files? Thanks
>
> Note: The member variable can be static if that makes it easier; the
> data inside "m_navi" never changes once it is set, and there is only
> one instance of "MyClass" ever created; I would like to set the value
> inside the constructor though if possible since values in "m_navi"
> potentially gets updated between builds, so I'd only have to change
> the ".cpp" file, but this isn't 100% nessisary.
>
> I'm using Eclipse 3.2.2, on a Ubuntu 8.10 Machine. g++ Version
> "4.2.4". Thanks.


You have a typo in your source ("Myclass" vs. "MyClass")
One of the possible solutions would be the following:

// in header file:
class MyClass
{
public:
MyClass() {}
virtual ~MyClass() {}
private:
static const char* const m_navi[1][1];
};

// the following must not be placed in the header file:
const char* const MyClass::m_navi[1][1] = { { "1" } }; // static init

int main()
{
MyClass m;
return 0;
}

 
Reply With Quote
 
 
 
 
NvrBst
Guest
Posts: n/a
 
      11-13-2008
On Nov 12, 7:15*pm, "Adem" <for-usenet...@alicewho.com> wrote:
> "NvrBst" wrote:
>
> > I'd like to do something like this but having a problem with the
> > proper syntax in the constructor, maybe someone knows the correct
> > syntax?

>
> > ---MyClass.h---
> > #ifndef MYCLASS_H_
> > #define MYCLASS_H_

>
> > class MyClass {
> > public:
> > * *MyClass();
> > * *virtual ~MyClass();
> > private:
> > * *const char* const m_navi[1][1];
> > };

>
> > #endif /*MYMENU_H_*/
> > ----------

>
> > ---MyClass.cpp---
> > #include "MyClass.h"

>
> > MyClass::Myclass() {
> > * *m_navi = { {"1"} };
> > }

>
> > MyClass:~MyClass() {
> > }
> > ----------

>
> > 1. *I get an "expected ';' before '{' token" error at the "m_navi = {"
> > line. *I also get an "uninitalized member "Myclass::t" error.
> > 2. *If I change "m_navi = {" to "MyClass::m_navi = {" I get the same
> > errors as "1.".
> > 3. *I've tried declaring it in the header file directly (const char*
> > const m_navi[1][1] = { {"1"} } and get an "a brace-enclosed
> > initializer is not allowed here before '{' token" error.
> > 4. I've tried making it static (static const char* const m_navi[1][1]
> > = { {"1"} } and get the same errors as "3."
> > 5. If I move the (const char* const m_navi[1][1] = { {"1"} } into
> > the "main.cpp" file (above the "int main() {" line) then it compiles
> > fine and I can use it fine in the main function.

>
> > Question: Whats the proper way to assign the (const char* const m_navi
> > [1][1]) inside the constructor? *Assuming the above "MyClass.h" and
> > "MyClass.cpp" files? *Thanks

>
> > Note: The member variable can be static if that makes it easier; the
> > data inside "m_navi" never changes once it is set, and there is only
> > one instance of "MyClass" ever created; I would like to set the value
> > inside the constructor though if possible since values in "m_navi"
> > potentially gets updated between builds, so I'd only have to change
> > the ".cpp" file, but this isn't 100% nessisary.

>
> > I'm using Eclipse 3.2.2, on a Ubuntu 8.10 Machine. *g++ Version
> > "4.2.4". *Thanks.

>
> You have a typo in your source ("Myclass" vs. "MyClass") *
> One of the possible solutions would be the following:
>
> // in header file:
> class MyClass
> * {
> * * public:
> * * * MyClass() {}
> * * * virtual ~MyClass() {}
> * * private:
> * * * static const char* const m_navi[1][1];
> * *};
>
> // the following must not be placed in the header file:
> const char* const MyClass::m_navi[1][1] = { { "1" } }; * // static init
>
> int main()
> * {
> * * MyClass m;
> * * return 0;
> * }


Thank you That worked dandily. I think I got mixed up since I
kept trying to do it in either the header file, or the ctor. Thanks
once more.
 
Reply With Quote
 
NvrBst
Guest
Posts: n/a
 
      11-13-2008
Another Initialization Question if Possible; Kind of relates to the
top. Is there a short hand way to initializat during a new operator?
For example I can do the following:


----- CAN DO -----
const char** FSETUP[2] = {
new const char*[2],
new const char*[4]
}
int main() {
FSETUP[0][0] = "T1";
FSETUP[0][1] = "T2";
//ETC
return 0;
}

----- WOULD LIKE TO DO BUT ERRORS -----
const char** FSETUP[2] = {
new const char*[2] {"T1","T2"},
new const char*[4] {"F1","F2","F3","F4"}
}
int main() {
return 0;
}


It has to stay a jagged array. All examples online I find intalizae
the jagged array the long way, is there a short way to set it when it
is being initialized? The error is "Expected ';' before '{'")
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57