C++“函数”未在此范围内声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15217338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ "Function" was not declared in this scope
提问by Jonny Henly
I was given an assignment in class to:
我在课堂上接到了一项任务:
Write a program that creates and manages arrays by the use of pointers. Your main program should interact with pointers/arrays only through various functions. These functions will then limit the use of array to their proper handling. For example these functions will eliminate the possibility of overstepping the boundaries of an array.
编写一个程序,通过使用指针创建和管理数组。您的主程序应该仅通过各种函数与指针/数组交互。然后,这些函数会将数组的使用限制为正确处理。例如,这些函数将消除超出数组边界的可能性。
So far I have written the program and fixed most of the errors, however no matter what I try I keep getting "function not declared in this scope" errors. The program includes the following files: main.cpp
MyArray.cpp
and MyArray.h
到目前为止,我已经编写了程序并修复了大部分错误,但是无论我尝试什么,我都会收到“未在此范围内声明的函数”错误。该程序包括以下文件:main.cpp
MyArray.cpp
和MyArray.h
main.cpp
主程序
#include <iostream>
#include "MyArray.h"
using namespace std;
int main(int argc, char * argv[]) {
int *Array1, *Array2;
int Array1Size(10), Array2Size(10);
int Array1Value(0), Array2Value(0);
cout << "Enter Size and Initial value for Array1: ";
cin >> Array1Size >> Array1Value;
cout << "Enter Size and Initial value for Array2: ";
cin >> Array2Size >> Array2Value;
CreateArray(Array1, Array1Size);
InitArray(Array1, Array1Size, Array1Value);
CreateArray(Array2, Array2Size);
InitArray(Array2, Array2Size, Array2Value);
cout << "Printing Array1:" << endl;
PrintArray(Array1, Array1Size);
cout << endl;
cout << "Printing Array2:" << endl;
PrintArray(Array2, Array2Size);
cout << endl;
if(CopyArray(Array1, Array1Size, Array2, Array2Size)) {
cout << "Copy was successful" << endl;
} else {
cout << "Copy was not successful" << endl;
}
cout << "Printing Array1:" << endl;
PrintArray(Array1, Array1Size);
cout << endl;
EnterArray(Array1, Array1Size);
// MergeArray(Array1, Array1Size, Array2, Array2Size);
cout << "Printing Array1:" << endl;
PrintArray(Array1, Array1Size);
cout << endl;
return 0;
}
MyArray.h
MyArray.h
#ifndef _MyArray_h_included_
#define _MyArray_h_included_
class MyArray
{
public:
void CreateArray(int *array, int size);
/*
CreateArray(int *array, int size): Will accept a pointer to an array, and the size of the array and will allocate the appropriate amount of memory for the array
*/
void InitArray(int *array, int size, int value);
/*
InitArray(Array1, Array1Size, Array1Value): Will initialize the contents of the arry Array1 to the value Array1Value
*/
void PrintArray(int *array, int size);
/*
PrintArray(Array1, Array1Size): Will print the contest of the Array1
*/
bool CopyArray(int *arrayOne, int sizeOne, int *arrayTwo, int sizeTwo);
/*
CopyArray(Array1, Arrary1Size, Array2, Array2Size): Will copy the contents of Array2 to Array1 iff the two arrays are of equal size. If the copying process is successfully completed, then a value of 1 is returned, otherwise a value of 0 is returned.
*/
void EnterArray(int *array, int size);
/*
EnterArray(Array1, Array1Size): Will populate the Array1 from the stdin by prompting the user to enter all elements of the array.
*/
};
#endif
MyArray.cpp
MyArray.cpp
#include <iostream>
#include "MyArray.h"
using namespace std;
void MyArray::CreateArray(int *array, int size) {
array = new int[size];
}
/*
CreateArray(int *array, int size): Will accept a pointer to an array, and the size of the array and will allocate the appropriate amount of memory for the array
*/
void MyArray::InitArray(int *array, int size, int value) {
for(int index(0); index < size; index++) {
array[index] = value;
}
}
/*
InitArray(Array1, Array1Size, Array1Value): Will initialize the contents of the arry Array1 to the value Array1Value
*/
void MyArray::PrintArray(int *array, int size) {
for(int index(0); index < size; index++) {
cout << "Value of index: " << index << " is " << array[index] << endl;
}
}
/*
PrintArray(Array1, Array1Size): Will print the contest of the Array1
*/
bool MyArray::CopyArray(int *arrayOne, int sizeOne, int *arrayTwo, int sizeTwo) {
if(sizeOne != sizeTwo) {
return false;
} else {
for(int index(0); index < sizeTwo; index++) {
arrayOne[index] = arrayTwo[index];
}
return true;
}
}
/*
CopyArray(Array1, Arrary1Size, Array2, Array2Size): Will copy the contents of Array2 to Array1 iff the two arrays are of equal size. If the copying process is successfully completed, then a value of 1 is returned, otherwise a value of 0 is returned.
*/
void MyArray::EnterArray(int *array, int size){
int index(0);
while(index < size) {
int value(0);
cout << "Enter the value for index: " << index + 1 << endl;
cin >> value;
array[index] = value;
}
}
/*
EnterArray(Array1, Array1Size): Will populate the Array1 from the stdin by prompting the user to enter all elements of the array.
*/
The errors I get resemble this: main.cpp:'LineNumber': error. 'Every Function in MyArray.cpp' was not declared in this scope
(I would copy paste the exact errors but I can't copy paste from the terminal I'm using to connect to the remote host were I compile my code)
我得到的错误类似于:(main.cpp:'LineNumber': error. 'Every Function in MyArray.cpp' was not declared in this scope
我会复制粘贴确切的错误,但我无法从我用来连接到远程主机的终端复制粘贴,因为我编译了我的代码)
I've been trying to fix this problem for hours and I can't figure it out, I've even looked at other Stack posts like 'foo' was not declared in this scope c++
Any help would be much appreciated, thank you.
我一直在尝试解决这个问题几个小时,但我无法弄清楚,我什至查看了其他 Stack 帖子,例如“foo”未在此范围内声明 c++
任何帮助将不胜感激,谢谢。
Also I did not write main.cpp, it was given with the assignment.
另外我没有写main.cpp,它是随作业给出的。
回答by jean
In C++, a function foo
of a class A
can be invoked by A.foo()
or A_ptr->foo()
, just a foo()
will not work.
在 C++ 中,foo
类的函数A
可以通过A.foo()
or调用A_ptr->foo()
,只是 afoo()
不起作用。
I think you don't need a class here, just use free functions.
我认为您在这里不需要课程,只需使用免费功能即可。
If you do want to use a MyArray
class, there's no need to write int *Array1, *Array2;
in main.cpp and pass it as an arg. It maybe better to store the array in MyArray
class.
如果您确实想使用一个MyArray
类,则无需int *Array1, *Array2;
在 main.cpp 中编写并将其作为 arg 传递。将数组存储在MyArray
类中可能更好。
Just like this.
像这样。
main.cpp
主程序
#include <iostream>
#include "MyArray.h"
using namespace std;
int main(int argc, char * argv[]) {
//int *Array1, *Array2;
MyArray Array1;
int Array1Size(10);
int Array1Value(0);
cout << "Enter Size and Initial value for Array1: ";
cin >> Array1Size >> Array1Value;
Array1.CreateArray(Array1Size);
Array1.InitArray(Array1Size, Array1Value);
cout << "Printing Array1:" << endl;
Array1.PrintArray(Array1Size);
cout << endl;
return 0;
}
MyArray.h
MyArray.h
#ifndef _MyArray_h_included_
#define _MyArray_h_included_
class MyArray
{
public:
void CreateArray(int size);
void InitArray(int size, int value);
void PrintArray(int size);
private:
int *m_array;
};
#endif
MyArray.cpp
MyArray.cpp
#include <iostream>
#include "MyArray.h"
using namespace std;
void MyArray::CreateArray(int size) {
m_array = new int[size];
}
void MyArray::InitArray(int size, int value) {
for(int index(0); index < size; index++) {
m_array[index] = value;
}
}
void MyArray::PrintArray(int size) {
for(int index(0); index < size; index++) {
cout << "Value of index: " << index << " is " << m_array[index] << endl;
}
}
I ignored some functions in your solution as well as the destructor, where you should use delete[]
我忽略了您的解决方案中的一些函数以及您应该使用的析构函数 delete[]
回答by user1952500
Get a book on C++ and study it. You need to create a MyArray object and call the functions using the object:
找一本关于 C++ 的书并研究它。您需要创建一个 MyArray 对象并使用该对象调用函数:
MyArray myArr;
myArr.CreateArray(Array1, Array1Size);
etc
等等
If main is given, your createArray will have to Create the Array internally:
如果给出了 main,则您的 createArray 必须在内部创建数组:
MyArray myArr;
void CreateArray(int *array, int size)
{
return myArr.CreateArray(array, size);
}
回答by AnatolyS
First of all you have understand what class is. In your case to invoke your MyArray functions you have to have an instance of the MyArray class:
首先你要明白什么是类。在您调用 MyArray 函数的情况下,您必须拥有 MyArray 类的实例:
MyArray a;
a.CreateArray(...);
...