Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:21:50+00:00 2026-06-17T22:21:50+00:00

I am writing a member function for a class where I would like to

  • 0

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;i<this->n_vox;++i)                                         
        cout << tmp[i] << ' ';                                                 
}         
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:21:52+00:00Added an answer on June 17, 2026 at 10:21 pm

    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 << tmp[i] << ' ';                                                 
    }
    

    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 cout<< statements anyway, move that loop into the if clauses. Use curly braces to enclose them!.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a simple vector class and I would like to have some
I am writing a class in which I want to create member function template
I'm writing a delegate class but it fails to take const member functions. Here
I'm writing some attendance software. Each member will have an ID card with a
Consider a class X with N member variables, each of some copiable and movable
In Matlab, I would like to perform some operations on private members of a
I am writing a program in C++ and I would like to define priority
Possible Duplicate: default method argument with class property? I'm writing a recursive function and
Possible Duplicate: Why C# is not allowing non-member functions like C++ Instead of writing
I'm writing a class that uses the result of a few non-member functions (which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.