C++ - 错误 : 'function' was not declared in this scope

标签 c++ function include friend declare

我有以下问题: 错误:“kleiner”未在此范围内声明 我的教授告诉我,我的代码对他来说效果很好。 这些目录都包含在 bulid 选项中(我正在使用 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

A name first declared in a friend declaration within class or class template X becomes a member of the innermost enclosing namespace of X, but is not accessible for lookup (except argument-dependent lookup that considers X) unless a matching declaration at the namespace scope is provided - see namespaces for details.

我想你和你的教授有不同的编译器?

关于C++ - 错误 : 'function' was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24416857/

相关文章:

c++ - 位交换和指针

c - 让静态函数变量在C中取参数值

c - 在将 sqlite3 包含到我的项目中时遇到问题,makefile 没有执行我想要的操作

c++ - 如果递归函数定义为内联会发生什么?

PHP 将一个网站包含到另一个网站中

c++ - 为什么 g++ 10.1 会提示头文件中的命名 lambda 而其他人则不会?

c++ - 来自虚函数的 undefined reference

c++ - Qt C++ 项目的简单安装程序,仅针对 Windows

c++ - 涉及模板化转换运算符和隐式复制构造函数的歧义

javascript如何从事件函数返回一个值给外部函数