IntelliSense: "count" is ambigous - c++

#include "stdafx.h"
#include <iostream>
using namespace std;
void func(void);
static int count = 10; /* Global variable */
int main() {
while(count--) {
func();
}
return 0;
}
// Function definition
void func( void ) {
static int i = 5; // local static variable
i++;
cout << "i is " << i ;
cout << " and count is " << count << endl;
}
can't seem to fix this, just learning and reading Storage class in tutorialspoint.com . is this Visual Studio issue? because the code is working on Code::Blocks

There is a "count" function in std namespace, so it collides with you variable
You have several options:
1. Rename your variable to something else
2. Use "::count" instead of "count" (:: means global namespace and not std)
3. Don't do "using namespace std;", instead write "std::" in-front of everything that comes from std, for example: "std:cout", "std::endl"

Related

How to access the unnamed namespace with scope resolution?

I have this:
#include <iostream>
using namespace std;
// Variable created inside namespace
namespace first
{
int val = 500;
}
namespace
{
int val = 400;
}
// Global variable
//int val = 100;
int main()
{
// Local variable
int val = 200;
// These variables can be accessed from
// outside the namespace using the scope
// operator ::
cout << first::val << '\n';
cout << ::val << '\n';
cout << val << '\n';
return 0;
}
The ::val in this case will provide val = 400. But if I remove the comment to the global variable then the global namespace will be reached by ::val. So, in that case, how can I access the unnamed namespace?
int val = 400;
This is the output with
// Global variable
int val = 100; //Not commented.
500
100
200
A possible solution is to expand the unnamed namespace with a reference to the "hidden" variable:
#include <iostream>
// Variable created inside namespace
namespace first
{
int val = 500;
}
namespace
{
int val = 400;
}
// Global variable
int val = 100;
namespace {
int& access_to_val=val;
}
int main()
{
using namespace std;
// Local variable
int val = 200;
// These variables can be accessed from
// outside the namespace using the scope
// operator ::
cout << first::val << '\n';
cout << ::val << '\n';
cout << val << '\n';
cout << access_to_val << '\n';
return 0;
}
Check the code with Godbolt.
The output of the program is:
500
100
200
400
As a side remark: avoid using namespace std in the header.

OOP can't get value from a class

So I read a file in a function and set values to a class. I would like to read those same values in another function (another .cpp file) and I can't get it to work.
This is the code where I read values from .txt file. This seems to work. I can cout the value that I read.
#include "branjeDatoteke.h"
#include "parametri.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
void branjeDatoteke() {
Parametri pin[101];
string line;
ifstream myfile("pin.txt");
if (myfile.is_open())
{
for (int i = 0; i <= 100 && getline(myfile, line); i++)
{
pin[i].setPin(line);
// cout << pin[i].readPin() << endl;
//cout << line << '\n';
}
myfile.close();
// cout <<"tole more delat: "<< pin[2].readPin() << endl;
}
else cout << "Unable to open file";
}
And this is the code where I want to get the same values again, but cout is not working. I just get blank console where the cout should be.
#include <iostream>
#include "pin.h"
#include "parametri.h"
#include <string>
#include "branjeDatoteke.h"
using namespace std;
void pinPass() {
Parametri pin[101];
string pinKoda;
branjeDatoteke();
cout << pin[0].readPin() << endl;
cout << "Vnesite pin: ";
cin >> pinKoda;
for (int i = 0; i <= 100; i++) {
if (pin[i].readPin() == pinKoda) {
cout << pin[i].readPin() << endl;
cout << "KODA JE PRAVILNA" << endl;
}
else if (i > 100) {
cout << "kode ni v sistemu" << endl;
}
}
}
Assuming your Parametri class is correct, the issue is you are using local variables so they are initialised every time you call the function. They are allocated on the stack, locally for the calling function and can't be used outside of the function that declares them, at least not the way you're doing it. If you call the function twice you also have to assume all local variables must be reinitialised. One way you could solve this would be promoting your pin variable to global, like so:
// your_file_one.cpp
Parametri pin[101];
void PinPass() {
...
}
If you want to use it in another cpp file, then you have to redeclare the variable in the other file as well, like follows:
// your_file_two.cpp
extern Parametri pin[101];
The extern keyword specifies the variable was declared in another compilation unit - for simplicity let's imagine each C++ file which is not a header file as a separate compilation unit.
So your code will look like:
#include "branjeDatoteke.h"
#include "parametri.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
Parametri pin[101];
void branjeDatoteke() {
string line;
ifstream myfile("pin.txt");
if (myfile.is_open())
{
for (int i = 0; i <= 100 && getline(myfile, line); i++)
{
pin[i].setPin(line);
// cout << pin[i].readPin() << endl;
//cout << line << '\n';
}
myfile.close();
// cout <<"tole more delat: "<< pin[2].readPin() << endl;
}
else cout << "Unable to open file";
}
And
#include <iostream>
#include "pin.h"
#include "parametri.h"
#include <string>
#include "branjeDatoteke.h"
using namespace std;
extern Parametri pin[101];
void pinPass() {
string pinKoda;
branjeDatoteke();
cout << pin[0].readPin() << endl;
cout << "Vnesite pin: ";
cin >> pinKoda;
for (int i = 0; i <= 100; i++) {
if (pin[i].readPin() == pinKoda) {
cout << pin[i].readPin() << endl;
cout << "KODA JE PRAVILNA" << endl;
}
else if (i > 100) {
cout << "kode ni v sistemu" << endl;
}
}
}
There are better ways of using global variables than declaring them many times and you may want to research these if you're going to write bigger programs. Also global variables are very useful in certain instances but must not be abused as they can make bigger applications much more difficult to read and maintain.
The Parametri array in your pinPass function is empty(or more precisely , has garbage values).You call the branjeDatoteke function from within pinPass , the
branjeDatoteke function then creates it's own Parametri array (WHICH IS DIFFERENT from the one in your pinPass function),reads the values from the file and displays it via cout.
When branjeDatoteke is done with it's work , all the local variables of that function , inlcuding the Parametri array are destroyed and your program jumps back to the pinPass function.
To do what you're trying to achieve , which is , presumably , have a common array for both the functions, you can either pass the array from pinPass to branjeDatokete , or you can tell branjoDatokete to allocate an array on the heap and then return a pointer to it.I guess the first approach fits better for what you're trying to achieve.

Transfering Variable between different files [duplicate]

This question already has answers here:
When to use extern in C++
(4 answers)
Closed 3 years ago.
I've got aud.ccp, aud.h, geist.ccp, geist.h. In geist.ccp I've got a variable which needs to get to aud.ccp.
If I got:
int x = 5;
in geist.ccp, how can I achieve it, that a 8 gets represented in the console when I use
cout << x+y << endl;
as well as
cin >> y; // ofc I enter 3 here.
in aud.ccp.
Edit:
I wrote:
int x
in the public part of geist.h
and I wrote:
x = 5;
in geist.cpp.
Finaly I wrote
extern int x;
in aud.cpp
But somehow I do not get the result I want
You need to declare the variable in a public scope of one module:
int x;
and declare its use in another one:
extern int x;
Then both modules, when linked together, will use the same variable.
It's most conveniently done with the defining declaration (with an optional initializer) placed in a .cpp module, and the extern declaration put into a .h file. Then each module, both the one defining the variable and those importing it, see the same extern declaration, which guarantees the declaration is same as an actual definition of the variable.
You have to care about "redefinition x variable Error" in your code.
You can Try this method:
geist.h:
#ifndef GEIST_H
#define GEIST_H
int x {5};
#endif
geist.cpp:
#include "geist.h"
#include <iostream>
using namespace std;
void printname()
{
cout << "The X value is" << x <<"\n";
}
aud.h:
#ifndef AUD_H
#define AUD_H
extern int x;
void Add_X_with_User_Desire();
#endif
aud.cpp:
#include "aud.h"
#include <iostream>
using namespace std;
void Add_X_with_User_Desire()
{
int y{0};
cout << "Please Enter an Integer Number: "<< "\n";
cin >> y;
cout << "y + x: " << x+y<<"\n";
}
and finally main function:
stack59228825.cpp:
#include <iostream>
#include "aud.h"
int main()
{
std::cout <<"X variable in main function is:" <<x << "\n";
Add_X_with_User_Desire();
x = 10;
std::cout << "targetVariable in main function is:" << 10 << "\n";
Add_X_with_User_Desire();
}

How to access to anonymous namespace variable if the same variable exists in global

Let's imagine situation:
#include <iostream>
int d =34;
namespace
{
int d =45;
}
int main()
{
std::cout << ::d ;
return 0;
}
Here the output is 34, because :: means global namespace. But If I comment 3rd line the output is 45, which is strange.
If I use std::cout << d ; - I get error
s.cxx:12:15: error: reference to ā€˜dā€™ is ambiguous
How can I access unnamed_namespace::d in this scenario?
PS: I've read that unnamed namespace is used for static global variables aka visible only in file scope
You cannot disambiguate between the two ds in main without the aid of something else.
One way to disambiguate between the two is to create a reference variable in the namespace and then use the reference variable in main.
#include <iostream>
int d = 34;
namespace
{
int d = 45;
int& dref = d;
}
int main()
{
std::cout << dref << std::endl;
return 0;
}
But then, why confuse yourself with the same variable? If you have the option, use a different variable name in the namespace or give the namespace a name.
namespace
{
int dLocal = 45;
}
int main()
{
std::cout << dLocal << std::endl;
std::cout << d << std::endl;
return 0;
}
or
namespace main_detail
{
int d = 45;
}
int main()
{
std::cout << main_detail::d << std::endl;
std::cout << d << std::endl;
return 0;
}

C++ accessing global variables/objects in a namespace with a variable/object with the same name

#include <iostream>
#include <string>
using namespace std;
string a;
namespace myNamespace
{
string a;
void output()
{
cout << a << endl;
}
}
int main()
{
a = "Namespaces, meh.";
myNamespace::a = "Namespaces are great!";
myNamespace::output();
}
The result is "Namespaces are great!". So is there any way to access the global string a inside of the namespace myNamespace instead of just the local one?
Like this:
void output()
{
cout << ::a << endl; //using :: = the global namespace
}