C++ :: Friend Functions - Variable Out Of Scope
Oct 11, 2014
I'm trying out friend functions not in just one source file to see how it works but I'm getting an error.
/* ----- ClassOne.h ----- */
#ifndef CLASSONE_H_
#define CLASSONE_H_
#include "classTwo.h"
using namespace std;
class ClassOne {
[Code] ....
Basically, I just want to print out the private members of ClassOne using ClassTwo's friend function twoPrintsOne().
The error is in classTwo.cpp and it says that m_a and m_b in the twoPrintsOne function are not declared in this scope.
View 3 Replies
ADVERTISEMENT
Sep 3, 2014
Are there any special considerations when using friend functions and namespaces?
objid.h
#pragma once
namespace FOO
{ typedef ULONGobjid_t;
class OBJID
{FOO::objid_tm_objnum;
[Code] ....
My C++03 compiler is giving me the following error:
C:devDATALIBOBJID.cpp(42,14) : error (346) : member "FOO::OBJID::m_objnum" is inaccessible
Is this a compiler issue, or am I missing something here trying to use a friend function? If I don't put the OBJID class in a namespace, everything compiles fine.
View 2 Replies
View Related
May 3, 2013
Can an overloaded operator be a friend function?
View 7 Replies
View Related
Dec 29, 2013
I have a situation: I have a class character
class Character;
class Village;
class Character {
public:
//Functions
void charGen(); //Character creation
[Code] .....
According to the Friendship and Inheritance tutorial [URL] ...., that code should work, but it doesn't. I am given an error: undefined reference to questsCompleted
The compiler I am using is Code::Blocks ....
View 4 Replies
View Related
Jan 31, 2014
Ran into something today that does not make sense:
This compiles: Code: int x = 5;
switch(x) {
case 0:
{
int value = 5;
}
break;
[Code] ....
Ok so it doesn't like int value = 6 b/c of int value = 5 for case 0. However since the value in case 0 is declared within the brackets one would think it has case scope.
So I tried this:
Code: int x = 5;
switch(x) {
case 0:
{
int value = 5;
}
break;
[Code] ....
Now it doesn't like it b/c value has not been declared in case 1:. These two conditions cannot possibly be both true at the same time. You cannot disallow the declaration of value in case 1 b/c it interferes with value in case 0 and at the same time disallow me to use value from case 0 b/c it is not in scope. If it was not in scope then theoretically I should be able to declare value in case 1.
Both MSVS 2012 and 2013 exhibit the same behavior. I checked the standard and it is unclear on the matter.
View 7 Replies
View Related
Jan 27, 2014
I'm working through this neural network tutorial, unfortunately I get stuck trying to compile on line 28, saying "error: 'neuronNum' not declared in this scope." I seem to always get stuck on these kinds of errors, yet I don't understand because I though that the variable was declared and initialized within the for loop.
#include <iostream>
#include <vector>
using namespace std;
[Code]....
View 5 Replies
View Related
Jun 15, 2014
//I dont understand this why does "<< "
The value of global now is: " << global << "
";" is equals to nine
#include <iostream>
int subtract (int a, int b);
int global = 5;
int main(void) {
using std::cout;
int a, b;
[Code] ....
View 1 Replies
View Related
Aug 8, 2013
Code:
#include <iostream>
using namespace std;
void f() {
int x=17;
//cout<<main::y<<endl; i want to access y from main scope
}
int main() {
int y=23;
//cout<<f::x<<endl;
I want to access x from f scope is there any way for this without global declaration? specially about function scopes...
View 1 Replies
View Related
Nov 17, 2013
is there any variable type(or with another keyword) that we can change it's value in global scope?
View 5 Replies
View Related
Jun 14, 2013
Are there any situations to explicitly use the scope resolution operator with global scope? I can imagine a situation like:
#include <cmath>
class IntWrapper{
public:
IntWrapper& pow(char);
IntWrapper(char);
private:
int m_int;
[Code] ....
But then I would think that the writer should have used a different name, and that using the scope resolution operator in the constructor body is still pointless...
View 8 Replies
View Related
Mar 27, 2014
Some coding standards document to follow for naming a variable, functions and using braces for if etc and other important standards i have to follow while writing the code. Right now in a hurry to finish the program i am naming a variable what ever comes to mind which is creating a lot of problems for me.
View 2 Replies
View Related
Apr 21, 2014
Write a function write with variable number of arguments that takes a string first argument followed by any number of arguments of type double and prints on the screen a string formatted by the rules described below. The first argument may contain formats in curly braces of the form {index[:specifier]}, where the square brackets show optional parts (this is :specifier may be missing), and index is the sequence number of an argument of type double (starting from sequence number 0).
Rules for formatting: In the printed string the curly brackets and their content will be replaced by the argument with the given index, formatted according to the given format specifier. If the format specifier is missing, the argument will be printed with its default format. For example:
write("The number {0} is greater than {1}.", 5, -3);
will print
The number 5 is greater than -3.
write("There are no format specifiers here.");
will print
There are no format specifiers here.
The format specifiers and their meanings are listed in the following table
Specifier MeaningFormat Output for 1.62 Output for 2.0
none default {0}1.62 2
ccurrency{0:c}$1.62 $2.00
escientific{0:e}1.620000e+000 2.000000e+000
ffixed point{0:f}1.620000 2.000000
iround to int{0:i}2 2
Limitations: You may limit the maximum number of arguments your function can process to a certain value, for example 10.
Suggested extensions:
-Add an optional alignment specification in the format , e.g., make the format of the form {index[,alignment][:specifier]}, where alignment is an integer specifying the width of the field in which the corresponding argument will be printed. If alignment is positive, align to the right, if it is negative, align to the left.
-Accept an optional integer after the specifier letter, specifying the required precision in the output. For example, {0:f2} will print the number 1.6234 as 1.62, but {0:f5} will print it as 1.62340.
View 1 Replies
View Related
Aug 31, 2014
So I have a class object that contains the private member variable spot and the public member function MoveLock. Within MoveLock, is a member variable called numbers that holds the place where a user is on a "lock knob". Now, what I'm trying to accomplish is that whenever the user turns the "knob" in the wrong direction, the position is updated with that current numbers so that the clicks needed to unlock the first state is also updated. But I get these errors:
Error E2096 C:Users...switchtest.cpp 34: Illegal structure operation in function main()
Error E2294 C:Users...switchtest.cpp 39: Structure required on left side of . or .* in function main()
Ultimately, what I have in main() is a piece of what I'm going to implement in a class member function. I'm also thinking about moving the if else statements out of the for and creating a second one for the else portion.
#include <iostream>
#include <windows.h>
#include <iomanip>
using namespace std;
HANDLE inKeys = GetStdHandle(STD_INPUT_HANDLE);
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
[code]....
View 10 Replies
View Related
Mar 13, 2014
Ok so when the program runs the first function the data is stored and displayed in the file. The second function is supposed to read the name entered, compare it to the ones in the file then take the price with it BUT I seem to have done something wrong when reading the files (or maybe it has to do with the global function I'm not sure). Here's parts of the code :
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
[Code]....
View 7 Replies
View Related
Mar 30, 2013
[URL] ..... Prototype is commented.
[URL] ..... Prototype is included.
both give the correct output. Why?
View 2 Replies
View Related
Sep 19, 2013
The code below doesn't compile. Two things to clear up:
1) I know x is going to be garbage.
2) I used the same type name label ElementType.
#include <iostream>
#include <ostream>
template <typename ElementType>
class Example {
[Code] .....
View 3 Replies
View Related
Mar 23, 2013
If yes then how?
View 6 Replies
View Related
Jul 24, 2013
What is the role of friend function in this program? Is it even executed here?
#include <iostream>
using namespace std;
class loc {
int longitude, latitude;
public:
loc() {} // needed to construct temporaries
[Code] ....
View 2 Replies
View Related
Nov 9, 2013
Consider the following program below, which compiles without a hitch on MinGW GCC 4.8.1:
template <typename T>
class Outer {
class Nested1 {};
template <typename U>
class Nested2
[Code] .....
Is there any way I can move the definition of func outside of the class?
View 1 Replies
View Related
Jul 13, 2012
Can't quite seem to get this figured out...
Code:
template <typename T>
class Foo {
public:
Foo(const T& t) :
t(t)
[Code] ....
I can't provide a generic implementation of the << operator, it depends on the specific types of T. So how do I specialize the operator for a specific type T ?
View 5 Replies
View Related
Jan 21, 2015
Am trying to write table object into file. Here's the source code
.hpp file
class Table {
private:
int table_no;
std::string table_type;
bool engaged;
std::time_t start_time;
double total_sec;
[Code] ....
When i compile the above code i get the following error...
table.hpp: In function ‘std::ifstream& operator>>(std::ifstream&, Table&)’:
table.hpp:19:7: error: ‘int Table::table_no’ is private
table.cpp:91:12: error: within this context
table.hpp:20:15: error: ‘std::string Table::table_type’ is private
table.cpp:92:12: error: within this context ...........
View 4 Replies
View Related
Nov 23, 2014
I'm trying to understand the basics of oop ...
#include <iostream>
using namespace std;
template <typename T>
class max_vector {
private:
T* elemente;
int lungime;
[Code] ....
The purpose of this program is to overload two different operators one inside the class, and the other one outside using friend. The problem is that i get 1 error at the '*' one.
View 1 Replies
View Related
Dec 26, 2014
I want to overload prefix and postfix increment(++) operators with friend function. I also have to use the constructors for this. How can I do this? in C++
View 4 Replies
View Related
Mar 9, 2013
I need understanding this block of code, particularly this line : *getLeftChild() { return this - _child; }
Code:
public class UpperNode {
BOX _box;
int _child;
FORCEINLINE UpperNode *getLeftChild() { return this - _child; }
...
};
Here I have this function:
Code:
void
UpperNode::visulization(int level) {
if (isLeaf())
_box.visulization();
else
if ((level > 0)) {
[Code] .....
It also makes calls for "getLeftChild()";
But I see that getLeftChild expects function pointer, and I absolutely have no clue where "this" comes from inside function body.
(return this - _child) - "this" has to be integer.
Or, if we gave pointer, and "this" is referring to some UpperNode, then I can't understand to which one, I have no UpperNode array defined or something. So if this functions is actually scaling pointer address, then scaling where to? I could comprehend it, if I had some array of UpperNodes, but not just class. I have UpperNodes array defined in other friendly class, but don't think they are related .....
View 2 Replies
View Related
Nov 25, 2012
I am struggling to enable friendship between two classes in separate header files for a banking program.
I am attempting to get the Person class to use variables from the Account class. Heres what I have so far.
ACCOUNT.h:
Code:
#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
#include <string>
#include <math.h>
#include <windows.h>
#include <vector>
#include "Person.h"
using namespace std;
class person;
[code].....
View 3 Replies
View Related
Apr 22, 2013
Did a little Googling on this but couldn't find anything definitive. Is it safe to do something like
Code:
void MyClass::myFunc(){
my_type_t &foo = some_obj->get_member_reference();
store_for_later(&foo);
}
Then at some pointer later in execution, another function uses the pointer passed to store_for_later.
View 5 Replies
View Related