Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;-错误:';功能';未在此范围中声明_C++_Function_Include_Friend_Declare - Fatal编程技术网

C++ C++;-错误:';功能';未在此范围中声明

C++ C++;-错误:';功能';未在此范围中声明,c++,function,include,friend,declare,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:

我有以下问题: 错误:“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;
}
#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;
}
#包括
#包括“vector.h”
使用名称空间std;
int main(int argc,char*argv[])
{
向量v1;

不能在标头中的类定义之外声明friend函数。在类之外声明friend函数之前,该函数不可见。

在类之外声明函数并指定为friend

参考文献

在类或类模板X中的友元声明中首先声明的名称将成为X的最内层封闭命名空间的成员,但除非在命名空间范围中提供了匹配声明,否则无法进行查找(考虑X的参数相关查找除外)-有关详细信息,请参阅命名空间


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

不要把using指令放在头文件中。当它们包含头文件时,没有人希望被迫引入额外的内容。而且,如果有人将一个向量分配给另一个向量,结果会非常糟糕。只是一个辅助注释:使用英语编程,不要混淆东西。我已经说过了我和我的老雇主看到了德语/英语的混合,在他们决定与一家外国公司合作后,没有人能够理解代码。他使用的是visual studio,我在gnu gcc中使用的是code::blocks,但在另一个程序中(我喜欢的是,它主要是从他的代码中复制的)在类中声明的friend函数可以正常工作。在类外声明函数会是什么样子?但我认为我们不允许这样更改他的部分代码…他使用的是哪个版本?如果是早期版本,很可能不太一致。基本上是复制函数n声明(不带friend关键字)到类外,可能在类上方。
bool kleiner(const int&a,const int&b);
作为最后一条建议,请始终尝试使用尽可能现代的编译器检查代码,这确实有助于保持代码的干净和清晰。
#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;
}