C++ - 错误:'function' 未在此范围内声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24416857/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:43:54  来源:igfitidea点击:

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

c++functionincludefrienddeclare

提问by honiahaka10

I have the following Problem: error: 'kleiner' was not declared in this scope My Professor told me, that my code just works fine for him. The directories are all included in bulid options (I am using Code::Blocks). Can someone please tell me what the problem might be?

我有以下问题:错误:'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

向量.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

向量.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;
}

回答by Niall

Declare the function outside of the class as well as specifying as a friend.

在类外声明函数并指定为友元。

Reference; http://en.cppreference.com/w/cpp/language/friend

参考; 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.

首先在类或类模板 X 中的友元声明中声明的名称成为 X 最内部封闭命名空间的成员,但不可访问以进行查找(考虑 X 的依赖于参数的查找除外),除非命名空间范围内的匹配声明是提供 - 有关详细信息,请参阅命名空间。

I think you and your Professor have different compilers?

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

回答by Vlad from Moscow

Declare the friend function also outside the class definition in the header. It is not visiable until it will be declared outside the class.

也在头文件中的类定义之外声明友元函数。它是不可见的,直到它在类外声明。