Global Array in C++ - 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()
{
}

Related

Code doesn't display anything when it should display "Hello World!"

I am very very VERY new to coding with C++, but I have some experience with Python, and wanted to start to learn functions very early on because I know how much of a lifesaver they can be in the future, can anyone help me figure this out
here is my code
#include <iostream>
using namespace std;
int helloWorld()
{
cout << "Hello World!" << endl;
return 0;
}
int main()
{
int helloWorld;
return 0;
}
In this snippet:
int main()
{
// Uninitialized variable declaration, no function call
int helloWorld;
return 0;
}
You're just simply declaring an uninitialized integer variable, you are not calling the function anywhere.
You need to call it:
int main(void)
{
helloWorld(); // no need of 'int helloWorld()', 'void' is enough
return 0;
}

Calling a function via the main using a class

I'm trying to add 2 to a class variable using a function, but it gives me this undefined reference to addTwo(int) even though I already have it declared.
#include <stdio.h>
#include <iostream>
using namespace std;
class Test {
public:
int addTwo(int test);
int test = 1;
};
int addTwo(int test);
int main() {
Test test;
cout << test.test << "\n";
addTwo(test.test);
cout << test.test;
}
int Test::addTwo(int test) {
test = test + 2;
return test;
}
The defined member function int Test::addTwo(int test) do differ from the declared global function int addTwo(int test);, which the compiler searches for.
To eliminate the error, define the global function or change the call of the global function to call of the member function.
In order to "add 2 to a class variable using a function", you should stop shadowing the member variable by the argument. (You can use this->test for using member variable, but this won't be needed in this case)
Try this:
#include <iostream>
using namespace std;
class Test {
public:
int addTwo();
int test = 1;
};
int main() {
Test test;
cout << test.test << "\n";
test.addTwo();
cout << test.test;
}
int Test::addTwo() {
test = test + 2;
return test;
}
Since it is a member function of the instance test you have to call it as
test.addTwo(test.test);
Instead, you're calling it as
addTwo(test.test);
and it doesn't know what that function is. As far as the compiler is concerned, addTest(int) doesn't exist because you haven't defined it outside of the class definition.

Please help me with C++ Namespace

#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.

Why wouldn't the following program compile?

Why wouldn't the following code compile? Basically what is not right in the following code? I'm assuming that declaring the same variable twice without assigning any value would be the problem.
#include <iostream>
using namespace std; int foo() { return 1; }
int main() { int a; int a; cout << foo() << endl; return 0;}
remove one "int a;" declaration. Even if it was possible, there is no reason to do that.

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.