c++ pointer not declared

I am writing a member function for a class where I would like to print out some dynamically allocated array according to the string name passed to the member function. The following code somehow get a compilation error:

error: 'tmp' was not declared in this scope 
How should I do the coding? What is the problem of my code?
void backgrnd::print(const char m[])< if (m == "interior") int* tmp = this->interior; else if (m == "fB") float* tmp = this->fB; for (int i=0;in_vox;++i) cout
45.3k 9 9 gold badges 85 85 silver badges 102 102 bronze badges asked Jan 29, 2013 at 1:48 20.6k 31 31 gold badges 103 103 silver badges 140 140 bronze badges They're created in the if and else scopes, which are not accessible from the for loop scope. Commented Jan 29, 2013 at 1:49

Something like that might work if this were python, but in c++, each if/else and loop body has its own scope. Also, even if all your uses of tmp were in the same scope, you would get a redeclaration error, since you'd be declaring the name twice with different types.

Commented Jan 29, 2013 at 1:53

3 Answers 3

There are a few problems with your code.

You're passing a string by pointer (writing const char [] is quite the same as writing const char * – BTW, const is left associative and you could write it as char const * as well).

void backgrnd::print(const char m[]) 

Anyway, in the next line you're comparing this pointer m with the pointer refering to the string literal constant "interior"

 if (m == "interior") 

And this does not what you had most likely in mind. This operation compares the values of the pointers and not the strings! The pointers would only compare as equal, if the pointer you passed to the function was that of the same string literal "interior" , which is very unlikely the case. If it was just any other string, even if it also contained the character sequence interior , it would not compare equal. It would also happen if the compiler and/or linker didn't redact redundant string literals into a single pointer constant.

And of course the other pointer - string literal comparisions suffer from the same problem.

Now the next problem is, that you create a scoped pointer variable tmp , which you initialize with some instance class member pointer variable of the same type. But as soon as the scope is left that variable is no longer visible.

 int* tmp = this->interior; else if (m == "fB") float* tmp = this->fB; 

. and this for loop can no longer see it. Now this loop is problematic by itself, because it's unclear what n_vox means.

 for (int i=0; i < this->n_vox; ++i) 

I guess you wrote the above to save some code duplication below. The problem is: C++ is a statically typed language so the following statement can not "dynamically" type to the type of the tmp variable.

 cout

Here are a few suggestions:

when using C++ you should use std::string instead of naked char arrays. This also makes the equality operator == do what you naively expected. If you insist on using C-style char arrays, use a string comparision function like strncmp .

Since you need to write statically typed coutUse curly braces to enclose them!.