Questions tagged [non-member-functions]
98 questions
0
votes
1 answer
c++ --direct-- access of class members in non-member function bodies
The following example is obviously wrong, but I would like to know if it possible to achieve something like the following
extern int return_value();
class A {
private:
int k = 1;
public:
friend int return_value();
};
int return_value()
{
…

woosah
- 843
- 11
- 22
0
votes
2 answers
Choosing to make a function a member, non-member, private, public, etc
I've searched around for descriptions of the difference between member and non-member functions and, while I'm still quite confused, I thought I'd give an example to clear things up for me a bit. Here's a question from an old test our instructor…

FilT
- 5
- 1
-1
votes
1 answer
Free functions in C++
I want to add v2 to v1. My member function is working, but free function is not. How can I solve this problem, thanks.
When I compile with: clang++ -std=c++2a hw1.cpp -o hw1
and run with: ./hw1
give 5 as output.
#include
using namespace…

emir
- 9
- 4
-1
votes
2 answers
How do you call a non-member function with a template in c++, where the typename is only in the return?
My goal is to have a non member function use a template for a return value. This is so I can return a float array, a double array, etc. I get a "couldn't deduce template parameter 'T'" error.
Here is the method I am trying to use:
template

John Glen
- 771
- 7
- 24
-1
votes
3 answers
C++ free function
I have the following code
#include "stdafx.h"
#include
using namespace std;
#include "graderec.h"
int main( )
{
GradeRecord studentAnn("45-2791", 14, 49);
GradeRecord studentBob("67-5803",25, 50);
int bobsUnits;
int…

Jacob
- 21
- 1
- 6
-2
votes
2 answers
No operator ">>" matches these operands operand types are: std::istream >> double
For my project I'm trying to create a free function for a complex number class. It is defined in the cpp file. The function is an overloaded input streaming operator but I keep getting the error
No operator ">>" matches these operands operand types…

Muu
- 21
- 1
- 5
-2
votes
2 answers
What effect does const at the beginning of a non-member function declaration have?
Digging through MSDN, I ran into just another curious line:
// This function returns the constant string "fourth".
const string fourth() { return string("fourth"); }
The full example is buried here:…

sigil
- 815
- 8
- 16
-2
votes
2 answers
When/if to make a non-virtual function a member function
I am trying to get a feel for modern C++ idioms and best practices, and I wanted to ask if, when authoring a class, there was ever a time one should make a function a member function, instead of a free-function in the class's namespace, besides when…

user3867053
- 9
- 1