C++ - 错误:未在此范围内声明'function'
C++ - error: 'function' was not declared in this scope
我有以下问题:错误:未在此范围内声明'kleiner'我的教授告诉我,我的代码对他来说工作得很好。这些目录都包含在构建选项中(我使用Code::Blocks)。谁能告诉我是什么问题吗?
main.cpp
#include <iostream>
#include "vector.h"
using namespace std;
int main(int argc, char *argv[])
{
Vector v1;
cout << "v1: " << v1 << endl;
Vector v2(8);
cout << "v2: " << v2 << endl;
cout << "Minimum von v2: " << v2.min() << endl;
Vector v3(v2);
cout << "v3: " << v3 << endl;
cout << "Anzahl von v3: " << v3.getAnzahl() << endl;
if ( kleiner( v3[2], v2[5] ) )//<<--<<--<<-- HERE IS THE PROBLEM
cout << v3[2] << " ist kleiner als " << v2[5] << endl;
int arr[5] = { 10, 5, 2, 3, 12 };
Vector v4;
cout << "v4: " << v4 << endl;
v4.setVector( arr, 4 );
cout << "v4 nach set: " << v4 << endl;
cout << "Minimum von v4: " << v4.min() << endl;
cout << "Anzahl von v4: " << v4.getAnzahl() << endl;
return 0;
}
vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
using namespace std;
class Vector
{
int* v;
int anzahl;
public:
Vector(int anzahl = 10);
Vector( const Vector& vec ); // Kopierkonstruktor
~Vector();
friend bool kleiner( const int& a, const int& b );
int min() const;
int getAnzahl() const;
int operator[]( const int i ) const;
void setVector( int* sv, int sanzahl);
friend ostream& operator<< ( ostream& os, const Vector& v );
};
#endif
vector.cpp
#include "vector.h"
Vector::Vector( int a ) : anzahl(a)
{
v = new int[a];
for ( int i = 0; i < a; i++ )
v[i] = i;
}
Vector::Vector( const Vector& vec )
{
anzahl = vec.getAnzahl();
v = new int[anzahl];
for ( int i = 0; i < anzahl; i++ )
v[i] = vec[i];
}
Vector::~Vector()
{
delete[] v;
v = NULL;
}
bool kleiner( const int& a, const int& b )
{
return ( a < b );
}
int Vector::min() const
{
int min = v[0];
for ( int i = 1; i < anzahl; i++ )
{
if ( v[i] < min )
min = v[i];
}
return min;
}
int Vector::getAnzahl() const
{
return anzahl;
}
int Vector::operator[] ( const int i ) const
{
return v[i];
}
void Vector::setVector( int* sv, int sanzahl )
{
delete[] v; // alten Inhalt loeschen
anzahl = sanzahl;
v = new int[anzahl];
for ( int i = 0; i < anzahl; i++ )
v[i] = sv[i];
return;
}
ostream& operator<< ( ostream& os, const Vector& v )
{
for ( int i = 0; i < v.anzahl; i++ )
os << v[i] << ", ";
return os;
}
在类外部声明函数并指定为友元。
引用;http://en.cppreference.com/w/cpp/language/friend
我想你和你的教授有不同的编译器?首先在类或类模板X的友元声明中声明的名称成为X最内层封闭命名空间的成员,但不能用于查找(除了考虑X的参数依赖查找),除非在命名空间作用域中提供了匹配的声明——详细信息请参阅命名空间。
在头文件的类定义之外声明友元函数。
相关文章:
- 为什么使用与父级声明的 std::function 在与子级一起使用时会显示错误?
- "expression cannot be used as a function"回报声明
- 错误:"Function"未在此范围内声明
- 尝试使用参数包绑定 std::function 时获取未声明的参数
- 声明为无效的变量或字段'Function'
- 'function'的外联定义与'Class'中的任何声明都不匹配
- 编译器给出"function not declared in this scope"但声明了函数 (C++)
- 在头文件上为类声明 = 和 [] 运算符,"must be a nonstatic member function"错误
- 错误:未在此范围内声明'Friend Member Function Name'
- 在定义之前声明一个未定义的 std::function 的目的是什么?
- Mingw 'std::function' 尚未声明?
- 不断收到错误"no member function declared"即使它已声明和定义
- C++ - 错误:未在此范围内声明'function'
- 为什么 g++ 声明某些 valarray o 存在"no matching function for call cbegin(o)"<double>?
- 如何声明没有std::function的函数返回函数
- c++声明顺序(const/type/function)
- 如何正确声明以函数类型作为形参的模板(如std::function)
- C++ "Function"未在此范围内声明
- 如何在类声明范围内使用“using space::function”
- "Error: no matching function for call distance::distance()"......无法从 main 中的类声明对象?