So I am currently making a small prototype for a bigger project I'm working on and I've gotten completely stuck on it. I am rather new to c++ and haven't worked with headers or namespaces before ever. The issue is that when i try to use my created namespace it fails completely and the compiler (clang) returns undefined.
#include <iostream>
#include "bark.hpp"
using namespace bark;
int main() {
bark::woof();
}
Header file:
#pragma once
#ifndef FUNCTIONS_HPP
#define FUNCTIONS_HPP
namespace bark {
void woof();
}
#endif
file with functions:
#include <iostream>
#include "bark.hpp"
void woof() {
std::cout << std::endl << "woof" << std::endl;
}
In the implementation file, this definition:
void woof()
{
// ...
}
defines a function woof in the global namespace. So when you make the call:
bark::woof();
in the main function, there is no bark::woof defined, and the linker will refuse to link the program.
To correctly define the woof from namespace bark, you need to either qualify it:
void bark::woof()
{
// ...
}
or else introduce the namespace bark, and define it inside:
namespace bark
{
void woof()
{
// ...
}
}
Related
main cpp
#include "utility.h"
#include <windows.h>
int main()
{
do
{
changeColor();
system("pause");
} while (true);
}
utility cpp
#include "utility.h"
#include "variables.h"
#include <windows.h>
namespace utility
{
void changeColor()
{
if (var::colorCounter == 0)
{
system("color af");
}
else if (var::colorCounter == 1)
{
system("color cf");
}
else if (var::colorCounter == 2)
{
system("color df");
}
else if (var::colorCounter == 3)
{
system("color 6f");
}
else
{
system("color 9f");
var::colorCounter = -1;
}
var::colorCounter++;
}
}
utility header
#ifndef utility
#define utility
void changeColor();
#endif
variables
#ifndef variables
#define variables
namespace var
{
inline int colorCounter{};
}
#endif
idk whats causing it to produce the changeColor() to not define what should i do? also is may coding right?
There are many problems with your code which are shown through the comments in the modified program below.
main.cpp
#include "utility.h"
int main()
{
utility::changeColor();//used utility:: because you've to be in the scope of namespace utility to call function changeColor()
}
utility.h
#ifndef UTILITY_H //USED INCLUDE GUARDS
#define UTILITY_H
namespace utility {
void changeColor(); //this function declaration is now inside the utitliy namespace
}
#endif
variables.h
#ifndef VARIABLES_H //USED INCLUDE GUARDS
#define VARIABLES_H
namespace var
{
//NOTE the extern keyword here instead of inline keyword
extern int colorCounter; //this is a nondefininig declaraiton for colorCounter.
}
#endif
variables.cpp
#include "variables.h"
namespace var
{
int colorCounter = 0; //this is definition of colorCounter
}
The output of the above program can be seen here.
Modifications
Some of the modifications that i made include:
In main.cpp, you have to be in the scope of the namespace utility to call function changeColor(). This is achieved using utility::.
In utility.h, header guards are used. This is a recommended practice.
In utility.h, the function declaration for changeColor is placed inside the namespace utility.
In variables.h, extern keyword is used instead of inline keyword to make the declaration of colorCounter a declaration that is not a definition. This essentially means, colorCounter has external linkage.
In variables.cpp, the variable colorCounter has been initialized with value 0.
Note
If you still want to use inline instead of extern you can do so from C++17 and onward as can be seen here. So if you use inline your program will work for C++17 and onwards. But if you use extern as in my above code, then your program will work in all C++ versions. You can choose whichever version you want.
I created a new project in c++ but i keep getting the same error
Main.cpp
#include <iostream>
#include <string.h>
#include "Computer.cpp"
#include "Computer.h"
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
Computer.h
#ifndef COMPUTER_H_INCLUDED
#define COMPUTER_H_INCLUDED
#include <string>
class Computer
{
public:
std::string marca;
float prezzo;
bool acceso;
Computer();
void Accenditi();
void Spegniti();
void ImpostaMarca(std::string m);
void ImpostaPrezzo(float p);
};
#endif
Computer.cpp
#include "Computer.h"
Computer::Computer()
{
}
void Computer::Accenditi()
{
if(!acceso)
{
acceso = true;
}
else
{
std::cout << "Sono già acceso";
}
}
void Computer::Spegniti()
{
if(acceso)
{
acceso = false;
}
else
{
std::cout << "Sono già spento";
}
}
void Computer::ImpostaMarca(std::string m)
{
marca = m;
}
void Computer::ImpostaPrezzo(float p)
{
prezzo = p;
}
The Problem
i don't understand what's wrong with Computer.cpp, i keep getting "cout is not a member of std". I tryed to add "using namespace std" and i also tryed to add the library #include but i get a new file called "makefile.win". How can i fix this error ?
You need to include iostream header in your Computer.cpp file as such:
include <iostream>
and to make your life easier, you can also add:
using std::cout;
using std::endl;
right at the bottom of your include, that way you don't have to keep adding "std::cout" everytime, you can just use "cout"
Also want to add:
You can remove the include computer.cpp from your main.cpp and just leave the header. The C++ linker will automatically link your computer.h and computer.cpp together since .cpp includes the header, and your main includes the computer.h
Add # include <iostream> at the files that you use std::cout and std::cin.
I wrote down an example code to try to replicate the error I am getting in a school project about the scope of an object:
In file: classTest.cpp
#include "headerone.h"
#include "headertwo.h"
#include <iostream>
using namespace std;
int main() {
ClassOne* pntrObj1 = new ClassOne;
ClassTwo* pntrObj2 = new ClassTwo;
pntrObj1->testClassOne();
return 0;
}
In file: headerone.h
#ifndef HEADERONE_H
#define HEADERONE_H
#include "headertwo.h"
#include <iostream>
using namespace std;
class ClassOne {
public:
void testClassOne() {
cout << "One Worked\n";
pntrObj2->testClassTwo();
}
};
#endif
In file: headertwo.h
#ifndef HEADERTWO_H
#define HEADERTWO_H
#include <iostream>
using namespace std;
class ClassTwo {
public:
void testClassTwo() {
cout << "Two Worked";
}
};
#endif
To be clear, the error is: pntrObj2 was not declared in this scope. The error comes from the file headerone.h
If I had to guess, I need to somehow pass the reference but I am not sure where to start for that. Any help is appreciated.
The variable pntrObj2 is only visible inside the scope in which it was declared, in this case your function main(). In other words, only code inside the curly braces of main() would be able to use the name pntrObj2 to reference that variable. However you can pass that value to other pieces of code by making it the argument of a function call.
So maybe what you want to do is add an argument to the testClassOne() method, so you can pass in the value of pntrObj2. So pntrObj1->testClassOne(); would become pntrObj1->testClassOne(pntrObj2);, and where you define testClassOne you can add a corresponding parameter. I'll let you figure this out so as to not completely do your homework for you :)
Here you include your file a lot of time and in testClassOne function, you do not declare pntrObj2
use
void testClassOne() {
cout << "One Worked\n";
ClassTwo* pntrObj2 = new ClassTwo()
pntrObj2->testClassTwo();
}
insteed of
void testClassOne() {
cout << "One Worked\n";
pntrObj2->testClassTwo();
}
Recently I've been learning how to create methods within classes so that I only have to write a method once and for each of that class I instantiate I can call the one method and it will work only on the variables of the object that called it, I know how to do this when only using main.cpp and no headers however I am confused on how I should be writing this when I use a class header and cpp.
I have a sample of code similar to what I want to achieve:
#include <iostream>
using namespace::std;
class Object
{
public:
int stuff;
void manageStuff();
Object();
};
void Object::manageStuff()
{
stuff++;
}
Object::Object() : stuff(0) {}
Object object1, object2;
int main() {
for (int i = 0; i < 10; i++)
{
object1.manageStuff();
object2.manageStuff();
cout << object1.stuff << "\n";
cout << object2.stuff << "\n";
}
}
This works fine and allows me to have two instances of Object and a method that works independently for each instance, this is my current project:
main.cpp:
#include <iostream>
#include "Test.h"
using namespace std;
int main()
{
Test test;
for (int i = 0; i < 10; i++)
{
test.count(); // Here's my error "undefined reference to Test::count"
}
return 0;
}
Test.cpp
#include <iostream>
#include "Test.h"
using namespace std;
Test::Test()
{
//ctor
}
Test::~Test()
{
//dtor
}
Test.h
#include <iostream>
#ifndef TEST_H
#define TEST_H
class Test
{
public:
Test();
virtual ~Test();
void count();
int counter();
};
#endif // TEST_H
and finally TestFunctions.h
#include <iostream>
#include "Test.h"
#ifndef TESTFUNCTIONS_H_INCLUDED
#define TESTFUNCTIONS_H_INCLUDED
void Test::count()
{
Test::counter++;
std::cout << Test::counter;
}
#endif // TESTFUNCTIONS_H_INCLUDED
I'm sure that there will be something that's very obviously wrong to a more seasoned programmer and I'm about to look a bit thick but any help would be greatly appreciated
Thanks!
I would suggest getting rid of TestFunctions.h, and adding the implementation of Test::count() to Test.cpp. Currently, the TestFunctions.h header is not included anywhere, so you have no access to the definition from main.
You defined (i.e. implemented) Test::count() in a header file (TestFunctions.h), but you never included it anywhere so the code there is not compiled.
You should change it to be in a .cpp file, compile it and link it with the other source files. There's no reason why not to place it in Test.cpp.
Rename TestFunctions.h into TestFunctions.cpp, make it compiled same way as main.cpp and linked.
Alternatively, include TestFunctions.h somewhere, e.g. main.cpp
How to use namespaces in C++ where it is accessible in different header files. Lets say I have this below:
// namespaces
#include <iostream>
using namespace std;
namespace first
{
int var = 5;
}
namespace second
{
double var = 3.1416;
}
int main () {
cout << first::var << endl;
cout << second::var << endl;
return 0;
}
and I want t use var variable from first namespace in another class... that is defined and implemented in another .h and .cpp file?
//server.h
#ifndef SERVER_H
#define SERVER_H
class server{
server();
//blah
};
#endif SERVER_H
//server.cpp
server::server()
{
first::var = 3;
}
is this possible to do it like this? When I try I get an error that says that my namespace is not defined. And if i put using namespace first in the .h or .cpp it says there is no namespace called first...
Besides having the namespace in a header, you need to make the variable extern:
//header.h
namespace first
{
extern int var;
}
//implementation.cpp
#include "header.h"
namespace first
{
int var = 5;
}
If the variable is not extern, a symbol will be generated wherever the header is included, and you'll get linker errors.
If you don't want the extra header, you can just declare the variable as extern in the same namespace where you want to use it:
//server.cpp
namespace first
{
extern int var;
}
server::server()
{
first::var = 3;
}
Note some answers might claim that you should make the variable static. This is wrong, although it will compile, as then the variable won't act as a global. A copy of it will be created for every translation unit.