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.
Related
I'm having an issue where I am trying to use variables stored in a struct called from a header file. I am able to call to each cpp file ie., call an action from one to another however variables are proving difficult.
Client.cpp
#include <iostream>
#include "Header.h"
#include <vector>;
#include <conio.h>;
#include <string>;
using namespace std;
int main()
{
cout << "Please enter a name: " << flush;
cin >> sNam.sNames;
main2();
return 0;
}
Admin.cpp
#include <iostream>
#include "Header.h"
#include <vector>;
#include <conio.h>;
#include <string>;
using namespace std;
void main2()
{
if (sNam.sNames == sNam1.sNames1)
{
cout << "Correct Entry...\n";
}
else if (sNam.sNames != sNam1.sNames1)
{
cout << "Incorrect Entry...\n";
}
}
Header.h
#pragma once
#include "Admin.cpp"
#include "Client.cpp"
struct Names
{
string sNames;
string sNames1 = "John";
}sNam, sNam1;
don't include cpp files
also sNam and sNam1 are defined in both cpp files, you will have link time error
include Header.h in both Client.cpp and Admin.cpp
then declare what's common in Header.h
#pragma once
// #include "Admin.cpp"
// #include "Client.cpp"
struct Names
{
string sNames;
string sNames1 = "John";
};
extern Names sNam, sNam1;
void main2();
in one of cpp files
#include "Header.h"
Names sNam, sNam1;
I have 2 two files called main.cpp and Volum_sumar.cpp. I included Volum_sumar.cpp in main.cpp header, but it doesn't see global variables in main.cpp
Can someone tell where is my mistake?
//main.cpp
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <string>
#include <windows.h>
#include "Volum_sumar.cpp"
using namespace std;
fstream f("Figuri.txt");
fstream d("Dimens.txt");
int n=0;
struct Sfere
{
string codsf;
char culoare [15];
char material[15];
float xc,yc,r,arie,volum;
} sf[100],aux;
int main()
{
cazul3();
}
// Volum_sumar.cpp
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <string>
#include <windows.h>
using namespace std;
void cazul3(){
double volt=0;
for(int i=0;i<n;i++)
{
volt=volt+sf[i].volum;
}
cout<<"VOLUMUL SFERELOR INREGISTRARE ESTE DE : "<<volt<<"cm3"<<endl;
}
You are going about this all wrong.
Like #CruzJean said, by including Volum_sumar.cpp directly in main.cpp, you are trying to access n and sf before they have even been defined.
#include'ing cpp files in other cpp files is bad practice. You should #include only header files. You are supposed to declare shared items in header files that cpp files can #include at needed, then compile your cpp files individually, and then finally link the resulting object files together to make the final executable file.
Global variables that need to be accessed across cpp files need to be instantiated in one cpp file and declared as extern in other files. The linker will resolve the extern references.
Try something more like this instead:
shared.h
#ifndef shared_h
#define shared_h
#include <string>
struct Sfere {
std::string codsf;
char culoare [15];
char material[15];
float xc, yc, r, arie, volum;
};
extern Sfere sf[100];
extern int n;
#endif
main.cpp
#include <fstream>
#include "shared.h"
#include "Volum_sumar.h"
std::fstream f("Figuri.txt");
std::fstream d("Dimens.txt");
int n = 0;
Sfere aux;
int main() {
cazul3();
}
Volum_sumar.h
#ifndef Volum_sumar_h
#define Volum_sumar_h
void cazul3();
#endif
Volum_sumar.cpp
#include <iostream>
#include "shared.h"
#include "Volum_sumar.h"
void cazul3() {
double volt = 0;
for(int i = 0;i < n; ++i) {
volt = volt + sf[i].volum;
}
std::cout << "VOLUMUL SFERELOR INREGISTRARE ESTE DE : " << volt << "cm3" << std::endl;
}
I'm trying to create a function that calculates the average of all the numbers in my array. But when I run the code the vector in my header says its undeclared. What should I change ?
I have tried putting #include in my header file and using namespace std; but it still doesn't fix my problem. I have also tried passing my function as reference.
Source.cpp
#include <iostream>
#include <string>
#include "math.h"
#include <vector>
using namespace std;
int main()
{
vector<int> notes;
notes.push_back(8);
notes.push_back(4);
notes.push_back(3);
notes.push_back(2);
cout << average(notes) << '\n';
}
math.cpp
#include "math.h"
#include <vector>
using namespace std;
int average(vector<int> tableau)
{
int moyenne(0);
for (int i(0); i < tableau.size(); i++)
{
moyenne += tableau[i];
}
return moyenne / tableau.size();
}
math.h
#ifndef MATH_H_INCLUDED
#define MATH_H_INCLUDED
int average(vector<int> tableau);
#endif MATH_H_INCLUDED
Add #include <vector>.
Use std::vector instead of just vector.
While at it, change the argument type to const&.
#ifndef MATH_H_INCLUDED
#define MATH_H_INCLUDED
#include <vector>
int average(std::vector<int> const& tableau);
#endif MATH_H_INCLUDED
You need to add #include <vector> in math.h instead of in math.cpp
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.
I am getting a bunch of errors saying "Undefined reference to ...", and cant understand why. I have read other questions with the error "Undefined reference to ...", but the answers doesent work for me. Here is part of my code:
main:
#include <iostream>
#include <armadillo>
#include <string>
#include <DombsMain.h>
using namespace std;
using namespace arma;
int main(){
cout << "Armadillo version: " << arma_version::as_string() << endl;
dombsmain::initilize("test");
return 0;
}
dombsmain.h:
#ifndef DOMBSMAIN_H_INCLUDED
#define DOMBSMAIN_H_INCLUDED
#include <string>
#include <vector>
#include "Body.h"
#include "Constraint.h"
#include <Solver.h>
namespace dombsmain {
extern void initilize(std::string fileName);
extern std::string inputfileName;
...blabla
}
#endif // DOMBSMAIN_H_INCLUDED
dombsmain.cpp:
#include <DombsMain.h>
#include <BallJoint.h>
#include <dombs.h>
#indlude blabla
using namespace std;
using namespace arma;
namespace dombsmain{
void initilize(string infileName){
inputfileName = infileName;
..blabla
I think the error has something to do with namespace dombsmain. DombsMain.h is included both in main and in dombsmain.cpp, but it still says that the variables and functions in the namespace in undefined. I think it might be some conflict with including DombsMain.h int both main and dombsmain.cpp. I tried deleting the #include <DombsMain.h> in main.cpp, but then I couldent call dombsmain::initilize("test");
You just forgot to define std::string inputfileName. For example you can do it in dobsmain.cpp:
namespace dombsmain{
void initilize(string infileName){
inputfileName = infileName;
}
std::string inputfileName = "";
}