Headers file doesn't see global variables - c++

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;
}

Related

linking two cpp files whilst using a struct from a header file

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;

Included Class Type is apparently not declared, why?

I have the following issue thrown by the compiler:
include/FlowChannel.h:14:21: error: ‘LatticeCell’ was not declared in this scope
std::vector grid;
when having these 3 header files (LatticeCell.h, FlowChannel.h and Utilities.h) and 2 cpp files including them(lbm.cpp and Utilities.cpp):
LatticeCell.h
#ifndef LATTICECELL_H
#define LATTICECELL_H
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
/* Single cell */
using namespace std;
class LatticeCell{
private:
std::vector<double> matrix = {0,0,0,0,0,0,0,0,0};
unsigned int type; //fluid, no-slip, velocity or density
public:
//Constructor
LatticeCell(unsigned int inType){
type = inType;
}
};
#endif
FlowChannel.h
#ifndef FLOWCHANNEL_H
#define FLOWCHANNEL_H
#include <vector>
#include <string>
#include <cmath>
#include <iostream>
#include "LatticeCell.h"
using namespace std;
class FlowChannel{
private:
std::vector<LatticeCell> grid; //ERROR LINE
unsigned int dimX = -1;
unsigned int dimY = -1;
public:
FlowChannel(unsigned int nx, unsigned int ny){
dimX = nx+2;
dimY = ny+2;
unsigned int gridSize = dimX*dimY;
grid.reserve(gridSize);
initGrid(/*TODO Params*/);
}
};
#endif
lbm.cpp
#include <string>
#include <vector>
#include "LatticeCell.h"
#include "FlowChannel.h"
#include "Utilities.h"
int main(int argc, char** argv){
printsomething();
return 0;
}
Utilities.cpp
#include "LatticeCell.h"
#include "FlowChannel.h"
#include <string>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
void printsomething(){
cout << "something" << std::endl;
}
double calcRelaxationTime(unsigned int ny , double reynolds, double uin){
return 3.0 * (uin * ny / reynolds) - 0.5;
}
Utilities.h
#ifndef UTILITIES_H
#define UTILITIES_H
#include "LatticeCell.h"
#include "FlowChannel.h"
#include <vector>
#include <cmath>
void printsomething();
#endif
Further my compiler flags are:
-Wall -std=c++17 -pedantic
For some reason I can't figure out, why LatticeCell wouldnt be a declared class in FlowChannel, due to it being included. Do you guys know whats wrong?
Edit: I added lbm.cpp, Utilities.cpp and Utilities.h so you guys see the full scope of the problem
You should check if the files are in the same directory.
I copy and paste your code in VS 2019 and it work for me,
here are the pictures
FlowChannel LatticeCell
It seems, that deleting #include 'LatticeCell.h' everywhere but in FlowChannel.h. I dont get the error 100% to be honest, as this wouldn't execatly cause an include loop that would induce such an error, but it works.

How to fix ( 'vector' : undeclared identifier ) in my header file?

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

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.

Enum ifstream and getline()

I've got the problem with following code. It should get the object parameters from the txt file. Of course to get enum values correctly I've made this simple getline() function. I'm keep getting errors which I've tried to repair, but I still have no idea what's wrong. VC says that 'no instance of overloaded function getline matches the argument list'.
My code:
CKomputer.cpp
#include <iostream>
#include <cstdio>
#include <string>
#include <fstream>
#include "CKomputer.h"
#include "Procesor.h"
#include "KartaDzwiekowa.h"
#define TESTPR1
using namespace std;
void Komputer::pobierz(string nazwa)
{
#ifdef TESTPR1
cout<<"Uruchomiono metode pobrania stanu obiektu Komputer z pliku"<<endl;
#endif
ifstream plik_wejsciowy;
plik_wejsciowy.open(nazwa+".txt");
int zastos;
getline(plik_wejsciowy,linia);
zastos=atoi(linia.c_str());
zastosowanie=Komputer::zastosowanie(zastos);
plik_wejsciowy >> nazwa_komputera >> ram >> ile_kart_dzwiekowych >> zastos;
procesor.wczytaj(plik_wejsciowy);
if (ile_kart_dzwiekowych>0)
{
for (int i=0;i<ile_kart_dzwiekowych;i++)
karta_dzwiekowa[i].wczytaj(plik_wejsciowy);
}
plik_wejsciowy.close();
}
CKomputer.h
#pragma once
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
#include "UrzadzenieElektroniczne.h"
#include "Procesor.h"
#include "KartaDzwiekowa.h"
using namespace std;
enum zastosowanie{biznes, gaming, grafika, programowanie};
class Komputer: public UrzadzenieElektroniczne
{
private:
Procesor procesor;
KartaDzwiekowa *karta_dzwiekowa;
protected:
string nazwa_komputera;
int ram;
int ile_kart_dzwiekowych;
public:
Komputer();
Komputer(string nazwa_komputera2, int ram2, int ile_kart_dzwiekowych2);
Komputer(const Komputer& k);
Komputer(int ilosc_kart);
~Komputer();
static int ile_Komputerow;
static int licz_ile_Komputerow();
void komp_info();
void wlaczenieurz();
void zapisz(string nazwa);
void pobierz(string nazwa);
zastosowanie zastos;
};