Questions tagged [non-member-functions]
86 questions
0
votes
5 answers
Class scope and private members?
I am defining a function to add elements to the vector original_points, named void add_point(). Why is highlighting original_points as undefined, in the function body, when I am using type qualifier: friend (to gain access) and it is in the…
![](../../users/profiles/3313438.webp)
Ziezi
- 6,049
- 3
- 34
- 45
0
votes
1 answer
Correct way to implement operator!= for templates outside of class as a free function
I have a following template with inner classes in a map.hpp file:
template
class Map {
// public members of Map ...
class Iterator {
//public members of Iterator ...
friend bool…
![](../../users/profiles/642402.webp)
shiraz
- 1,148
- 2
- 11
- 25
0
votes
3 answers
c++ create, assign and compare a new variable to two object inside an Operator Overloaded function.
The assignment:
Implement an Alien class using a provided Alien.h file. An alien, in this scenario is described in terms of his/her height, weight, and gender. To compare two aliens, you use the following equation to determine the alien’s…
![](../../users/profiles/3010998.webp)
C Morgan
- 3
- 1
- 6
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()
{
…
![](../../users/profiles/1086137.webp)
woosah
- 803
- 11
- 19
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…
![](../../users/profiles/2217870.webp)
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…
![](../../users/profiles/14604477.webp)
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
![](../../users/profiles/13264143.webp)
John Glen
- 445
- 2
- 14
-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…
![](../../users/profiles/1605707.webp)
Jacob
- 21
- 6
-2
votes
1 answer
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…
![](../../users/profiles/13481403.webp)
Muu
- 11
- 3
-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:…
![](../../users/profiles/6885358.webp)
sigil
- 755
- 5
- 11
-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…
![](../../users/profiles/3867053.webp)
user3867053
- 9
- 1