Please help me with C++ Namespace - c++

#include <iostream>
#include <cstdlib>
namespace A {
int a = 10;
void get_value(){ std::cout << "a = " << a << std::endl; }
}
namespace B {
int b;
void get_value(){ std::cout << "b =" << b << std::endl; }
}
void set_B();
int main(){
using namespace A;
get_value();
set_B();
system("PAUSE");
return 0;
}
void set_B(){
using namespace B;
b = 15;
get_value(); // Why call to get_value() is ambiguous, error is not generated here ?
}
Why call to get_value() inside set_B() function is not ambiguous ( A::get_value() or B::get_value()) as both appear as ::get_value() inside set_B().

using namespace A; isn't active inside set_B because it only appears inside main. It is limited to the scope in which it appears: the body of main.

because A::get_value and B::get_value arn't normally visible in the global scope. the using namespace statement makes the declarations in that namespace visible inside set_B.

Related

Impact of namespaces on C++ template deduction priority

while trying to implement a metafunction, which needs to exist only if the "abs" function exists for some type, i ran into the folowing problem:
Here are two examples of code i would expect to yield the same results but in fact, they do not:
First example
#include <iostream>
#include <cmath>
using namespace std;
struct Bar{};
template<typename T>
int abs(T& v)
{
return -1;
}
void test()
{
Bar b;
double x = -2;
cout << abs(x) << endl;
cout << abs(b) << endl;
}
int main()
{
test();
}
Yields:
2
-1
Which is what i expect
Second example
#include <iostream>
#include <cmath>
namespace Foo
{
struct Bar{};
using namespace std;
template<typename T>
int abs(T& v)
{
return -1;
}
void test()
{
Bar b;
double x = -2;
cout << abs(x) << endl;
cout << abs(b) << endl;
}
}
int main()
{
Foo::test();
}
Yields:
-1
-1
Why using a namespace here is making the compiler prioritize the "local" abs methodthe over std::abs?
In the second case the using directive places declared names in the nominated namespace in the global namespace for unqualified name lookup. So within the namespace Foo the unqualified name abs declared in this namespace is found. That is the name abs declared in the namespace Foo hides the name abs declared in the global namespace.
From the C++ 14 Standard (7.3.4 Using directive)
2 A using-directive specifies that the names in the nominated
namespace can be used in the scope in which the using-directive
appears after the using-directive. During unqualified name lookup
(3.4.1), the names appear as if they were declared in the nearest
enclosing namespace which contains both the using-directive and the
nominated namespace. [ Note: In this context, “contains” means
“contains directly or indirectly”. — end note ]
The nearest enclosing namespace in the second program is the global namespace.
Below there are two more programs that demonstrate the same principle of the unqualified name lookup when a using directive is used.
#include <iostream>
void s() { std::cout << "The function s() called.\n"; }
struct s
{
s() { std::cout << "The constructor of struct s called.\n"; }
};
void test()
{
s();
}
int main()
{
test();
}
The program output is
The function s() called.
In this demonstration program the declaration of the function s hides the declaration of the structure s.
Second program.
#include <iostream>
namespace N1
{
void s() { std::cout << "The function N1::s() called.\n"; }
}
namespace N2
{
using namespace N1;
struct s
{
s() { std::cout << "The constructor of struct N2::s called.\n"; }
};
void test()
{
s();
}
}
int main()
{
N2::test();
}
The program output is
The constructor of struct N2::s called.
In this case the declaration of the structure with the name s hides the function with the same name s the declaration of which was placed in the global namespace due to the using directive.
For the reason why this takes place you can refer to Vlad's answer. However, if you want to still be able to perform the testing, you can do the testing in separate namespace.
#include <iostream>
#include <cmath>
namespace Foo
{
template<typename T>
int abs(T& v)
{
return -1;
}
}
namespace Test
{
struct Bar{};
using namespace std;
using namespace Foo;
void test()
{
Bar b;
double x = -2;
cout << abs(x) << endl;
cout << abs(b) << endl;
}
}
int main()
{
Test::test();
}
The output is
2
-1

How to call member functions from a class nested within a namespace?

I want to call a class member function that's nested within a namespace from a different file, but I don't know how.
For example:
How to call a class member function someFunc() that's located in code.h and nested within namespace "program" from main.cpp.
//code.h
#include <iostream>
namespace program
class test {
private:
int x;
public:
test()
{
test::x = 10;
};
someFunc()
{
cout << x << " ";
};
};
There are some problems in your code
#include <iostream>
namespace program { // <-- braces missing
class test
{
private:
int x;
public:
test()
{
test::x = 10; // <-- test:: is unnecessary but no error
};
void someFunc() // <-- return type missing
{
std::cout << x << " "; // <-- namespace std missing
};
};
} // <-- braces missing
int main() {
program::test t;
t.someFunc();
}

Global Array in C++

Can anybody tell me what is wrong in the following code when I initialize a global array and want to print its value outside main() function
#include <iostream>
using namespace std;
int global_array[5] = {10,20,30,40,50};
cout << global_array[2];
int main()
{
cout << "Hello World!" ;
}
The error keep popping is
error: 'cout' does not name a type|
The statement cout << global_array[2]; is not a declaration (it is an expression). Only declarations are allowed outside of functions.
So, if you want to print anything outside of main function, you can only do so by having the expression within another function.
I think the problem is that the code you have that does the printing is outside of any function. Statements in C++ need to be inside a function. For example:
#include <iostream>
using namespace std;
void hello();
int global_array[5] = {10,20,30,40,50};
void hello()
{
cout << global_array[2];
}
int main()
{
hello();
cout << "Hello World!" ;
}
Before asking a question, you can search: ‘cout’ does not name a type
Thanks you.
if you want to call it from outside the main it should be in a function something like this
#include <iostream>
using namespace std;
int global_array[5] = {10,20,30,40,50};
int pre()
{
cout << global_array[2];
return 0;
}
int x = pre();
int main()
{
cout<<"Hello World";
return 0;
}
as i mentioned it on comment it can be done via c++ classes.
#include <iostream>
int global_array[5] = { 10,20,30,40,50 };
struct foo
{
foo()
{
std::cout << global_array[2] << std::endl;
}
};
foo f;
int main()
{
}

A pointer to a bound function may only be used to call the function

I'm working on a homework assignment for my C++ class and have ran across a problem that I cannot figure out what I am doing wrong.
Just to note, the separation of the files is necessary and I realize this would be much easier if I just made a structure AttackStyles inside the main and forgo the additional class file altogether.
The base of my problem is that I cannot seem to be able to loop through an array of classes and pull out base data. Here is the code:
// AttackStyles.h
#ifndef ATTACKSTYLES_H
#define ATTACKSTYLES_H
#include <iostream>
#include <string>
using namespace std;
class AttackStyles
{
private:
int styleId;
string styleName;
public:
// Constructors
AttackStyles(); // default
AttackStyles(int, string);
// Destructor
~AttackStyles();
// Mutators
void setStyleId(int);
void setStyleName(string);
// Accessors
int getStyleId();
string getStyleName();
// Functions
};
#endif
/////////////////////////////////////////////////////////
// AttackStyles.cpp
#include <iostream>
#include <string>
#include "AttackStyles.h"
using namespace std;
// Default Constructor
AttackStyles::AttackStyles()
{}
// Overloaded Constructor
AttackStyles::AttackStyles(int i, string n)
{
setStyleId(i);
setStyleName(n);
}
// Destructor
AttackStyles::~AttackStyles()
{}
// Mutator
void AttackStyles::setStyleId(int i)
{
styleId = i;
}
void AttackStyles::setStyleName(string n)
{
styleName = n;
}
// Accessors
int AttackStyles::getStyleId()
{
return styleId;
}
string AttackStyles::getStyleName()
{
return styleName;
}
//////////////////////////////////////////////
// main.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include "attackStyles.h"
using namespace std;
int main()
{
const int STYLE_COUNT = 3;
AttackStyles asa[STYLE_COUNT] = {AttackStyles(1, "First"),
AttackStyles(2, "Second"),
AttackStyles(3, "Third")};
// Pointer for the array
AttackStyles *ptrAsa = asa;
for (int i = 0; i <= 2; i++)
{
cout << "Style Id:\t" << ptrAsa->getStyleId << endl;
cout << "Style Name:\t" << ptrAsa->getStyleName << endl;
ptrAsa++;
}
system("PAUSE");
return EXIT_SUCCESS;
}
My question is why do I get the error:
"a pointer to a bound function may only be used to call the function"
on both ptrAsa->getStyleId and ptrAsa->getStyleName?
I cannot figure out what is wrong with this!
You are missing () around the function calls. It should be ptrAsa->getStyleId().
You are missing parenthesis on both calls, it should be
ptrAsa->getStyleId()
to call the function.
ptrAsa->getStyleId
is used to refer to a member value / attribute.
You need to invoke the function, not merely reference it:
std::cout << "Style Id:\t" << ptrAsa->getStyleId() << "\n";
std::cout << "Style Name:\t" << ptrAsa->getStyleName() << "\n";
You are Forgot to put () in last in Your Function(ptrAsa->getStyleId ) Calling with arrow operator.

Possible to wrap or merge separate namespaces?

I seem to recall seeing notes somewhere on a way to combine multiple namespaces into one.
Now, looking for said notes I am not finding them -- even searching using search terms combing, grouping, merging and wrapping I'm not coming up with anything. Maybe I misunderstood what I saw before. I don't have a specific application for this, it's just a curiosity and it's a bit contrived.
But, starting with two name spaces...
namespace a {int func() {return 1;}}
namespace b {int func() {return 2;}}
I was looking for syntax to either simply wrap them in another name -- after the fact --
(yes, I know I can rewrite it in a nested way) or merge them into one new space. But, I did find that I if I add to one of the namespaces that much works.
namespace c {namespace a{ int func2() {return 3;}} }
int main(int argc, char **argv)
{
int a = a::func(); // normal case
int c = c::a::func2(); // wrapped and added to
//int c = c::func2(); // doesn't work
//int d = a::func2(); // doesn't work
}
The question are:
1) is there syntax that just combines the two spaces into one new one?
2) is there a syntax to wrap the spaces without adding more to the subspaces?
You can do this:
namespace c
{
using namespace a;
using namespace b;
}
But if a and b have elements with the same names, you won't be able to use them from namespace c.
The best solution since C++11 is:
namespace c
{
inline namespace a { using namespace ::a; }
inline namespace b { using namespace ::b; }
}
This way for names that not conflict you can qualify only by c and you can resolve conflicts by qualifing c::a or c::b.
e.g.:
namespace a
{
auto foo_a() { cout << "a::foo_a" << endl; }
auto foo() { cout << "a::foo" << endl; }
}
namespace b
{
auto foo_b() { cout << "b::foo_b" << endl; }
auto foo() { cout << "b::foo" << endl; }
}
namespace c
{
inline namespace a { using namespace ::a; }
inline namespace b { using namespace ::b; }
}
int main()
{
c::foo_a();
c::foo_b();
c::a::foo();
c::b::foo();
return 0;
}
You can wrap the namespaces in a new one like this:
namespace c {
namespace a { using namespace ::a; }
namespace b { using namespace ::b; }
}