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
Related
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.
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;
}
How I can sent string variable into function in other file?
main.cpp:
#include <iostream>
#include <conio.h>
#include <string>
#include "headers.hpp"
using namespace std;
int main()
{
string a;
cout<<"Type:"<<endl;
cin>>a;
other(a);
getch();
return( 0 );
}
headers.hpp:
#ifndef HEADERS_HPP
#define HEADERS_HPP
void other(string a);
#endif
function.cpp:
#include "headers.hpp"
#include <iostream>
#include <math.h>
using namespace std;
void other(string a){
cout<<a;}
I don't know why it doesn't work. Do you know solution?
If you are saying it does not compile, you have to move the #include <string> from main.cpp into headers.hpp
I got an error of C:\temp\hashTableProject\main.cpp|14|undefined reference to hash::Hash(std::string)
Anyone know how to solve this problem?
hash.h
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef HASH_H
#define HASH_H
class hash{
public:
int Hash(string key);
};
#endif // HASH_H
hash.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int hash::Hash(string key){
//int hash = 0;
int index;
index = key.length();
return index;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int main()
{
int index;
hash hashOb;
string traget = "Testing";
index = hashOb.Hash(traget);
cout << index << endl;
return 0;
}
Im using CodeBlock 13.12
There is only main.o file in obj folder. I dont know why the hash.o isn't there.
hash is an inbuilt template in "std" namespace.
Try removing using namespace std; lien and use namespace name as and when required.
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.