How to print something from similar h files using c++ - c++

i have 2 .h files:
first.h
namespace first
{
void foo() { std::cout << 1 << ","; }
void bar() { std::cout << 7; }
}
second.h
namespace second
{
void foo() { std::cout << 3 << ","; }
void bar() { std::cout << 9; }
}
and the main.cpp
#include <iostream>
#include "first.h"
#include "second.h"
......................
void main() {
foo();
bar();
}
The goal is that the main prints 3,7 using these h files above.
Does someone have an idea how I could do it? Thanks

You can call the functions using the namespace:
second::foo();
first::bar();
But if you want to use the same functions over and over you can bring them into your scope via
using second::foo;
using first::bar;
foo();
bar();

Related

Simple Class and Header in Separate File Won't Work

I'm currently learning c++ for a week and here's my problem:
run.cpp
#include <iostream>
#include "Abc.h"
int main(){
int a;
std::cout << "Enter a : ";
std::cin >> a;
// Object Initialization
Abc AbcObj();
}
the header, Abc.h :
#ifndef ABC_H
#define ABC_H
class Abc
{
public:
Abc();
protected:
private:
};
#endif // ABC_H
and finally my cpp file for implementation, Abc.cpp:
#include "Abc.h"
#include <iostream>
Abc::Abc()
{
std::cout << std::endl << "Object created ";
}
Why don't I get output on my console? I'm expecting "object created" should be on the console. These files are in the same directory.
You error doesn't come up because you've used different files, so I have used one in this example
struct Foo
{
int a;
Foo()
{
std::cout << "Constructor called!";
}
};
int main()
{
Foo obj();
}
Why don't you see the message? You can read this thread
The problem here is, Foo obj() is taken as a function declaration. To fix this you need to remove the ()
int main()
{
Foo obj;
}
Constructor called!

identifier is undefined c++ [duplicate]

#include <iostream>
using namespace std;
int main()
{
HelloWorld();
return 0;
}
void HelloWorld()
{
cout << "Hello, World" << endl;
}
I am getting the following compilation error with g++:
l1.cpp: In function 'int main()':
l1.cpp:5:15: error: 'HelloWorld' was not declared in this scope
You need to either declare or define the function before you can use it. Otherwise, it doesn't know that HelloWorld() exists as a function.
Add this before your main function:
void HelloWorld();
Alternatively, you can move the definition of HelloWorld() before your main():
#include <iostream>
using namespace std;
void HelloWorld()
{
cout << "Hello, World" << endl;
}
int main()
{
HelloWorld();
return 0;
}
You must declare the function before you can use it:
#include <iostream>
using namespace std;
void HelloWorld();
int main()
{
HelloWorld();
return 0;
}
void HelloWorld()
{
cout << "Hello, World" << endl;
}
or you can move the definition of HelloWorld() before main()
You need to forward declare HelloWorld() so main knows what it is. Like so:
#include <iostream>
using namespace std;
void HelloWorld();
int main()
{
HelloWorld();
return 0;
}
void HelloWorld()
{
cout << "Hello, World" << endl;
}
There is one more possibility for some reason nobody mentioned, namely using extern declaration:
#include <iostream>
using namespace std;
int main()
{
extern void HelloWorld();
HelloWorld();
return 0;
}
void HelloWorld()
{
cout << "Hello, World" << endl;
}
It's preferable when you don't want to introduce name of the function into namespace scope.
You need to have either a prototype of the function before the invocation or the whole function before it.
So the first is:
void HelloWorld();
int main() {
HelloWorld();
return 0;
}
void HelloWorld() {
cout << "Hello, World" << endl;
}
And the second way is:
void HelloWorld() {
cout << "Hello, World" << endl;
}
int main() {
HelloWorld();
return 0;
}
All these answers are correct, but strangely enough, this would have worked:
struct Hello {
static int main() { World(); return 0; } /* note: World() not declared yet */
static void World() { std::cout<<"Hello World"; }
};
int main() { return Hello::main(); }
You have to put the function before the main function.
in C++ you need to define or at least declare the functions before calling them.
What you are trying to do till now is something like this :
int main()
{
cout << b;
int b = 10;
}
So you can also trying like this :
#include <iostream>
using namespace std;
void HelloWorld();
int main()
{
HelloWorld();
return 0;
}
void HelloWorld()
{
cout << "Hello, World" << endl;
}
It is a good practice in C++ to define all other functions before the main function.
Rearrange HelloWorld() so that it appears before main():
#include <iostream>
using namespace std;
void HelloWorld()
{
cout << "Hello, World" << endl;
}
int main()
{
HelloWorld();
return 0;
}
If you're defining you're functioning below your main function you should declare it above first.
#include<iostream>
using namespace std;
void HelloWorld();
int main()
{
HelloWorld();
return 0;
}
void HelloWorld()
{
cout << "Hello, World" << endl;
}
Declare void HelloWorld() above main() method or add function prototype void HelloWorld(); so compiler can compile it . if the prototyped function does not exist then you get a linker error.
in this case
you are trying like this
std::cout <<hello<<endl;
std::string hello = "hello world";

When do you use which method to get variables from another file?

I am currently learning C++ and found that there are at least two ways using variables defined in other files. However I do not understand really, when to use what method.
For example:
I have writte in "h1.h":
extern int k;
and "a2.cpp" writes:
#include "a2.h"
#include "h1.h"
int k = 42;
int k2 = 43;
Then I can reference k in my main.cpp with:
#include "main.h"
#include "a1.h"
#include "h1.h"
#include <iostream>
Main::Main() {}
int main() {
std::cout << k << std::endl;
}
However if I want to use k2 in main.cpp I could simply write a getter/setter method, thereby I would avoid having to use extern in a common included header file.
I would like to know: What are other ways to access variables from other files? When do you use which method (and why )?
You expose k as a function, or not at all, not as a variable.
"h1.h":
int k();
void k2(int &);
"h1.cpp":
int k() { return 42; }
void k2(int & value) { value = 43; }
"main.cpp"
#include "h1.h"
#include <iostream>
int main () {
std::cout << k() << std::endl;
int any_name;
k2(any_name);
std::cout << any_name << std::endl;
}

Nifty counter in precompiled header does not work

To fix a problem caused by the static (de-)initialization order fiasco I tried to use the Nifty Counter idiom aka Schwartz Counter. I noticed, however, that it does not work if the header file containing the static initializer is included inside Visual Studio's precompiled header. In such a case I see in the debugger, that the constructor of the static initializer is called after the constructor of the other static object.
If I declare an additional static initializer right after #include "stdafx.h" or edit the precompiled header to not contain the declaration it works as expected.
Any ideas what might cause this?
EDIT
I was finally able to reproduce this problem in a new dummy project:
Foo.cpp
#include "stdafx.h"
#include "Foo.h"
#include <cassert>
#include <iostream>
#ifdef NIFTY
static int SchwartzCounter; // zero initialized at load time
static typename std::aligned_storage<sizeof(Foo), alignof (Foo)>::type foo_buf;
Foo& foo = reinterpret_cast<Foo&>(foo_buf);
#else
Foo foo;
#endif
Foo::Foo()
{
std::cout << __func__ << std::endl;
}
Foo::~Foo()
{
std::cout << __func__ << std::endl;
}
void Foo::doSomething()
{
std::cout << __func__ << std::endl;
assert(x == 42);
}
#ifdef NIFTY
FooInitializer::FooInitializer()
{
std::cout << __func__ << std::endl;
if (SchwartzCounter++ == 0)
{
new (&foo) Foo();
}
}
FooInitializer::~FooInitializer()
{
std::cout << __func__ << std::endl;
if (--SchwartzCounter == 0)
{
(&foo)->~Foo();
}
}
#endif
Foo.h
#pragma once
class Foo
{
public:
Foo();
~Foo();
void doSomething();
private:
int x = 42;
};
#ifdef NIFTY
extern Foo& foo;
static struct FooInitializer {
FooInitializer();
~FooInitializer();
} fooInitializer;
#else
extern Foo foo;
#endif
Bar.cpp
#include "stdafx.h"
#include "Foo.h"
#include "Bar.h"
#include <cassert>
#include <iostream>
#ifdef NIFTY
static int SchwartzCounter; // zero initialized at load time
static typename std::aligned_storage<sizeof(Bar), alignof (Bar)>::type bar_buf;
Bar& bar = reinterpret_cast<Bar&>(bar_buf);
#else
Bar bar;
#endif
Bar::Bar()
{
std::cout << __func__ << std::endl;
foo.doSomething();
}
Bar::~Bar()
{
std::cout << __func__ << std::endl;
}
void Bar::doSomething()
{
std::cout << __func__ << std::endl;
assert(x == 42);
}
#ifdef NIFTY
BarInitializer::BarInitializer()
{
std::cout << __func__ << std::endl;
if (SchwartzCounter++ == 0)
{
new (&bar) Bar();
}
}
BarInitializer::~BarInitializer()
{
std::cout << __func__ << std::endl;
if (--SchwartzCounter == 0)
{
(&bar)->~Bar();
}
}
#endif
Bar.h
#pragma once
class Bar
{
public:
Bar();
~Bar();
void doSomething();
private:
int x = 42;
};
#ifdef NIFTY
extern Bar& bar;
static struct BarInitializer {
BarInitializer();
~BarInitializer();
} barInitializer;
#else
extern Bar bar;
#endif
stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// If the following define is commented out, the nifty counter idiom is not used
#define NIFTY
// If the following include is commented out while the nifty counter idiom is used, the initialization order is correct
#include "Foo.h"

How do I declare a const in a header?

I want to test defining a const in a header and use it in functions, then call it. However I get the error, I added include guards which doesn't help. Error is: LNK1169: One or more defined multiply symbols found. How can i do it in a nother way? Is declaring const in .h and defining this const in .cpp and then including this .cpp in all other .cpps the only solution?
Header
#ifndef STORY
#define STORY
const int x = 4;
#endif
.cpp
#include <iostream>
#include "8-04.h"
void func1()
{
int w = x;
std::cout << "func1 " << w << std::endl;
}
.cpp
#include <iostream>
#include "8-04.h"
void func2()
{
int z = x;
std::cout << "func2 " << z << std::endl;
}
main
#include <iostream>
#include "8-04.h"
#include "8-04first.cpp"
#include "8-04second.cpp"
using namespace std;
int main()
{
func1();
func2();
}
The problem is that each .cpp includes the .h. This means that each .o contains a const int x. When the linker links these together, you get multiple definitions.
The solution is to modify the .h
#ifndef STORY
#define STORY
extern const int x; //Do not initialise
#endif
and in a single .cpp:
const int x=4
Edit:
I didnt even see the #include <file.cpp> business. Don't do that. Its horrible.
This should be like :
header.h:
#ifndef STORY
#define STORY
const int x = 4;
void func1();
void func2();
#endif
fun1.cpp
#include <iostream>
#include "header.h"
void func1()
{
int w = x;
std::cout << "func1 " << w << std::endl;
}
fun2.cpp
#include <iostream>
#include "header.h"
void func2()
{
int z = x;
std::cout << "func2 " << z << std::endl;
}
main.cpp
#include <iostream>
#include "header.h"
using namespace std;
int main()
{
func1();
func2();
}
You can not include ".cpp"
It can be done such as :
header.h:
#ifndef STORY
#define STORY
const int x = 4;
void func1();
void func2();
#endif
fun1.cpp
#include <iostream>
#include "header.h"
using namespace std;
void func1()
{
int w = x;
cout << "func1 value of w = " << w << "\n";
}
fun2.cpp
#include <iostream>
#include "header.h"
using namespace std;
void func2()
{
int z = x;
cout << "func2 value of z = " << z << "\n";
}
main.cpp
#include <iostream>
#include "header.h"
int main()
{
func1();
func2();
}
".cpp" file cannot be included in main source file.