English 中文(简体)
was not declared in this scope C++
原标题:

Why do I get this error in the code below?

class ST : public Instruction{
public:
ST (string _name, int _value):Instruction(_name,_value){}

    void execute(int[]& anArr, int aVal){
        //not implemented yet
        cout << "im an st" <<endl;
        anArr[value] = aVal;
    }
    virtual Instruction* Clone(){
        return new ST(*this);
    }
};



classes.h:81: error: ‘anArr’ was not declared in this scope
classes.h:81: error: ‘aVal’ was not declared in this scope
最佳回答

You have a problem with the type of the first parameter of your execute function. Read this up to know more about how to pass arrays around.

问题回答

Because the type of anArr is invalid.

Also you may be interested in using a covariant return type on your clone method. I.e. it can return a pointer to ST instead of Instruction.

Try this out :

void execute(int anArr[] , int aVal)

since You cant use array of reference .

If execute() is supposed to be taking an array of integers, you should probably declare it like this:

void execute(int* anArr, int anArrLength, int aVal)
{
   // ...
}

Note that there are several differences to your method:

  • anArr is passed in as a pointer to the start of the array. The client code can simply pass in the array variable name, as by definition this is equivalent to "a pointer to the start of the array".
  • anArrLength is passed in to indicate the length of the array. This is required to ensure that the execute() method doesn t access memory which is out of the bounds of the array (or what has been allocated for the array). Doing so could result in memory corruption.

You could improve the method signature above by adding a return value to indicate success or failure. This would allow the client code to detect if there have been any problems. For example:

// Returns true on success, false on failure
bool execute(int* anArr, int anArrLength, int aVal)
{
    // Get "value" through whatever means necessary
    // ...

    if (value >= anArrLength)
    {
        // Out of bounds of array!
        return false;
    }

    anArr[value] = aVal;

    // Do whatever else you need to do
    // ...

    return true;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签