C++ - error: 'function' was not declared in this scope
Declare the function outside of the class as well as specifying as a friend.
Reference; 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.
I think you and your Professor have different compilers?
honiahaka10
Updated on June 26, 2020Comments
-
honiahaka10 about 4 years
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?
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; }
-
honiahaka10 about 10 yearshe is using visual studio, i am using code::blocks with gnu gcc but in another program (which i, like that, mostly only copied from his code) it was just working fine with the friend function declared in the class. how would it look like with declaring the function outside of the class? but i don't think we are allowed to change his part of code like that...
-
Niall about 10 yearsWhich version is he working with? If it's an earlier version, it's likely that it is not too conformant.
-
Niall about 10 yearsBasically copy the function declaration (without the friend keyword) to outside the class, maybe above it.
bool kleiner( const int& a, const int& b );
-
Niall about 10 yearsAs a parting piece of advice, always try check code with as modern a compiler as you can, it really does help to keep the code cleaner and clearer.