friend function definition

Hans-Christian Stadler hans-christian.stadler@psi.ch
Tue Mar 27 15:32:00 GMT 2018


I've got a piece of code which I think should be legal, but I get an 
error with g++

#include <iostream>

class bird_watch {
     friend int register_bird() {
         return ++bird_watch::counter;
     }
     static int counter;
};

int main (int argc, char *argv[])
{
     std::cout << register_bird() << '\n';
}


stadler_h@tuxedo:~/Devel/Test/CPP$ g++ --version
g++ (Ubuntu 7.2.0-8ubuntu3.2) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

stadler_h@tuxedo:~/Devel/Test/CPP$ g++ -Wall -std=c++17 tmpl_test.cc
tmpl_test.cc: In function ‘int main(int, char**)’:
tmpl_test.cc:12:18: error: ‘register_bird’ was not declared in this scope
      std::cout << register_bird() << '\n';
                   ^~~~~~~~~~~~~

As far as my limited C++ knowledge goes, register_bird should be a valid 
function in global scope?

Really sorry to disturb if I'm mistaken,

HC



More information about the Gcc-help mailing list