Compiling Helper file with functions - c++

I'm at a loss - i'm just getting into C++ and for some reason this is not working out for me. So i'm using Netbeans, and i've got the following main file:
#include <cstdlib>
#include "functions.h"
using namespace std;
int main(int argc, char** argv) {
f("help");
return 0;
}
Functions.h file:
#include <string>
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
void f( string a );
#endif
and Functions.cpp file:
#include "functions.h"
void f( string a ) {
return;
}
So, long story short, it doesn't compile. It says it can't understand the string variable? I don't get it, i tried moving the include for string all over the place but nowhere seems to help. What do i do?

If you are attempting to use std::string, you have to #include <string> in your functions header, and call it std::string,since it is in the std namespace.
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <string>
void f( std::string a );
#endif
See this related post and also why is 'using namespace std' considered bad practice in C++?

You need to include string header file in Functions.h, also tell compiler that string is from std namespace.
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <string>
void f( std::string a );
#endif
Functions.cpp file:
#include "functions.h"
void f( std::string a ) {
return;
}
Better practice is to pass string by const reference
void f(const std::string& a ) {
return;
}
See Why is 'using namespace std;' considered a bad practice in C++?

Include the standard header: <string>
#include <string>

Related

std::any bad cast whenever a function inside a header is implemented in a cpp file returns it

This is my main.cpp
#include <iostream>
#include <unordered_map>
#include <string>
#include "myclass.h"
int main(int argc, const char * argv[]) {
any a = myclass::returnthis();
unordered_map<string, any>* hmap = any_cast<unordered_map<string, any>*>(a);
return 0;
}
this is myclass.h
#ifndef myclass_h
#define myclass_h
#include <any>
using namespace std;
class myclass {
public:
static any returnthis();
};
#endif /* myclass_h */
and this is myclass.cpp
#include "myclass.h"
#include <any>
#include <unordered_map>
#include <string>
any myclass::returnthis() {
return new unordered_map<string, any>();
}
Now, any_cast will report bad any cast. I am working on macOS and I have tried Xcode and CLion.
However, if I put the implementation of the function inside the header file, the problem vanishes. Why?

Undefined Reference when compiling C++

My code is similar to this one, but the problem is exactly the same: I'm getting an "undefined reference to `Test1::v" in Test1.cpp and in Test2.cpp when compilling the program in VSCode. What am I doing wrong? I'm a bit new on c++ so I just downloaded an extension that made me a project in c++ automatically. When I run the program using Ctrl + Shift + B it gives me this error, but when I do it with the Code Runner extension it doesn't detect the .cpp files.
// Test1.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST1_H
#define TEST1_H
class Test1{
public:
Test1();
static vector<Test1> v;
int a;
};
#endif
//Test1.cpp
#include "Test1.h"
Test1::Test1(){
a = 2;
v.push_back(*this);
}
//Test2.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST2_H
#define TEST2_H
class Test2{
public:
Test2();
double var;
};
#endif
//Test2.cpp
#include "Test2.h"
#include "Test1.h"
Test2::Test2(){
var = 5;
Test1::v[0].a += var;
}
//main.cpp
#include <iostream>
#include "Test1.h"
#include "Test2.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello world!" << endl;
}
You have declared the static vector in the header file, but you need to define it in a cpp file. Add:
vector<Test1> Test1::v;
to your test1.cpp file. You can learn more about definition vs declaration here.
Also make sure you read this: Why is "using namespace std;" considered bad practice?
You could prepend the class name to call the variable directly since it's static. So, you could do something like:
Test1::Test1(){
// v.push__back(*this); // previous
Test1::v.push_back(*this); // now
}
in Test1.cpp. You'll then get a reference tooltip on your VS Code:
static std::vector<Test1> Test1::v
Which proves it's done.

identifier "test" is undefined; What does this mean in this context

I am writing a weather station for a raspberry. I'm always getting the error message identifier "test" is undefined.
I've already tried to use no external class with a little example and this works perfectly. Now I'm trying to create an object test, but this doesn't work. I am always getting the error message:
E0020 identifier "test" is undefined
main.cpp:
#include <wiringPi.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <errno.h>
#include <stdlib.h>
include "MeasureWeather.h"
int main(void)
{
MeasureWeather test;
while (1)
{
test._setSensorPin(DHT_PIN);
}
return 0;
}
MeasureWeather.h:
#ifndef MeasureWeather
#define MeasureWeather
#include <wiringPi.h>
#include <stdio.h>
#include <iostream>
#include <errno.h>
#include <stdlib.h>
#include <cstring>
class MeasureWeather
{
public:
//setter for sensor pin
void _setSensorPin(uint8_t pin);
private:
uint8_t sensorPin;
};
#endif // !MeasureWeather
MeasureWeather.cpp:
include "MeasureWeather.h"
void MeasureWeather::setSensorPin(uint8_t pin)
{
_sensorPin = pin;
}
I hope somebody can help me with my issue. Thank you!
You have this at the top of your header file, as part of the include guard:
#define MeasureWeather
MeasureWeather is the name of your class. By defining it as a macro, you hide the class name. Thus the line
MeasureWeather test;
expands to
test;
which would be a reference to something called test, not a declaration.
Use a different identifier for your #include guard.

Multiple Class Files C++

I am unsure about the use of separate files for classes. How do I make functions inside the classes? Where do I put it?
QuizMain.cpp:
#include "QuizMain.h"
#include <iostream>
#include <string>
using namespace std;
QuizMain::QuizMain()
{
// Hia stackoverflow
}
QuizMain.h file:
#ifndef QUIZMAIN_H
#define QUIZMAIN_H
#include <string>
using namespace std;
class QuizMain
{
public:
QuizMain();
private:
};
#endif // QUIZMAIN_H
Main file:
#include <iostream>
#include <string>
#include "QuizMain.h"
using namespace std;
int main()
{
QuizMain qm;
return 0;
}
How would I make a class and call it correctly?
This is an example:
QuizMain.cpp file:
#include "QuizMain.h"
#include <iostream>
#include <string>
using namespace std;
QuizMain::QuizMain()
{
// Hia stackoverflow
}
void QuizMain::my_new_function(std::string my_name){
std::cout << "Hi " + my_name +"!" << std::endl;
}
QuizMain.h file:
#ifndef QUIZMAIN_H
#define QUIZMAIN_H
#include <string>
class QuizMain
{
public:
QuizMain();
void my_new_function(std::string my_name);
private:
};
#endif // QUIZMAIN_H
Main file:
#include <iostream>
#include <string>
#include "QuizMain.h"
using namespace std;
int main()
{
QuizMain qm;
qm.my_new_function("foo");
return 0;
}
Anyway, there is no point from asking such a question here. You should probably find a good book/resource and learn how to write and use functions.
Normally you have a header file and cpp file. The header file is where you declare your functions and member variables. The cpp file is where you implement your functions.
quizmain.h
// QuizMain.h file
#ifndef QUIZMAIN_H
#define QUIZMAIN_H
#include <string>
class QuizMain
{
public:
QuizMain(int quizScore);
// declare public functions here
private:
int quizScore; // declare private member variables here.
};
#endif // QUIZMAIN_H
cpp file
// QuizMain.cpp file
#include "QuizMain.h"
#include <iostream>
#include <string>
using namespace std;
QuizMain::QuizMain(int quizScore)
{
this.quizScore = quizScore; // init a quiz score
}
main
Call and create a class object like this
QuizMain quiz(95);
It is easy to do.
If you use the IDE project, the IDE set you to use the any file. Like code::block IDE But if you do not use the IDE project it is a little different to use.
You should write the .h file and then,after all writing you should put .cpp file.
Also you can use interface class that works by poiter.
/// .h file and declaration
#ifndef ONE.H
#define ONE.H
class one {
public:
one();
~one();
};
#include "one.cpp"
#endif ONE.H
then:
/// .cpp file and implementation
one::one(){
std::cout<<"constructor one"<<std::endl;
}
one::~one(){
std::cout<<"destructor one"<<std::endl;
}
then :
#include <iostream>
#include "one.h"
int main()
{
one o;
}
output:
constructor one
destructor one
Process returned 0 (0x0) execution time : 0.010 s
Press ENTER to continue.

Sharing a defined global variable in a header amongst two cpp files

I am interested in being able to share a defined global variable across two cpp files. Is the following possible? I am interested in this to avoid having to initialize the global shared variable multiple times. I am having trouble being able to build this code. How would you recommend to declare/define myMap in this case?
MapHeader.h
#ifndef _MAP_HEADER_
#define _MAP_HEADER_
#include <string>
#include <map>
using namespace std;
extern const map<int, string> myMap = {{100 , "one hundred"}, {200,"two hundred"}, {300,"three hundred"}};
#endif // _MAP_HEADER_
FirstFile.h
#ifndef _FIRST_FILE_
#define _FIRST_FILE_
#include "MapHeader.h"
#include <iostream>
#include <string>
using namespace std;
void myFunction1();
#endif
FirstFile.cpp
#include "FirstFile.h"
void myFunction1(){
cout << "myFunction1()" << myMap[100] << endl;
}
SecondFile.h
#ifndef _SECOND_FILE_
#define _SECOND_FILE_
#include "MapHeader.h"
#include <iostream>
#include <string>
using namespace std;
void myFunction2();
#endif
SecondFile.cpp
#include "SecondFile.h"
void myFunction2(){
cout << "myFunction2()" << myMap[200] << endl;
}
Main.cpp
#include "FirstFile.h"
#include "SecondFile.h"
int main()
{
myFunction1();
myFunction2();
return 0;
}
I am getting the error message:
error: passing 'const std::map<>' as 'this' argument of 'std:map<>' .....
In MapHeader.h, change your definition to extern map myMap; and then move your definition exactly as you had it in MapHeader.h into one of the .cpp's.