After writing my header file and trying to use it in the cpp.file. The compiler gives me an error when trying to redefine the function in header file.
I didn't face this problem the previous times I was using headers in a similar way. Maybe I initialize the Vector in a wrong way. Anyways here is the code:
#include <string>
#include <vector>
#include "lajitellut.h"
using namespace std;
namespace otecpp_lajitellut{
/*this is where the error appears*/
vector<string> lajitellut(int lkm, char*mjt[]){
vector<string> stringVector;
for(int i =0; i<lkm; i++){
stringVector.push_back(mjt[i]);
}
for(int i =0; i<lkm; i++){
for(int a = 0; a<lkm;a++){
if(stringVector[i] < stringVector[a]){
stringVector[i].swap(stringVector[a]);
}
}
}
return stringVector;
}
}
And here is the header file
#ifndef kissa
#define kissa
#include <string>
#include <vector>
namespace otecpp_lajitellut{
std::vector <std::string> lajitellut(int lkm, char* mjt[]) {
std::vector<std::string> stringVector;
return stringVector;
}
}
#endif // kissa
Put only the function declaration in the "lajitellut.h" header file:
#include <vector>
#include <string>
namespace otecpp_lajitellut {
std::vector<std::string> lajitellut(int, char*);
}
Put the function definition in the source "*.cpp" file:
#include <iostream>
#include <vector>
#include <string>
#include "lajitellut.h"
namespace otecpp_lajitellut {
std::vector<std::string> lajitellut(int lkm, char* mjt[]) {
// your code in here
}
}
int main(){
auto a = otecpp_lajitellut::lajitellut(10, "asd");
}
Note that definition is also a declaration. That being said you don't have a vector there. You have a function of type std::vector<std::string>. Don't use using namespace std;.
Ron is right.
Your function lajitellut() is already implemented in the .h file with the same signature. You can not create a double in the same namespace.
You can change the arguments or the type of the return value or change the namespace in the .cpp file.
Related
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'm getting an error everytime I compile the function.cpp file saying that stocks and newStock are not declared in this scope. I'm trying to use a struct inside a vector. Thanks for the help.
This is the main.cpp file
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <vector>
using namespace std;
struct Stocks
{
int one;
int two;
int three;
};
vector<Stocks> portfolio;
#include "testProject2.h"
int main()
{
buyStock(portfolio);
}
This is the header file.
#include <iostream>
void buyStock(vector<Stocks>& Portfolios);
This is the function.cpp file
#include <iostream>
#include <vector>
#include "testProject2.h"
void buyStock(vector<Stocks>& Portfolios)
{
Stocks newStock;
newStock{1,2,3};
Portfolios.push_back(newStock);
}
Your function.cpp file has no way to know what the Stocks struct is. Define it in the header file:
struct Stocks {
int one;
int two;
int three;
};
And remove its definition from main.cpp.
Also in your header file, you need
#include <vector>
and refer to vector parameter as std::vector<Stocks> &Portfolios (better than using namespace std;)
Your initialization syntax newstock{1,2,3} looks incorrect too.
You use vector in your header file without it being defined.
Try changing the header file to this:
#include <vector>
#include <Stocks.h> // name of .h file where Stocks is defined
void buyStock(std::vector<Stocks>& Portfolios);
// OR
using namespace std::vector;
void buyStock(vector<Stocks>& Portfolios);
I ran this code on a different IDE and it was successful. For some reason I get the above error message on Xcode. I assume I'm missing a header of some kind, but I'm not sure which one.
#include <iostream>
#include <limits>
#include <string>
#include <vector>
int main() {
vector<string> listRestaurants; // error: Implicit instantiation of undefined template
return 0;
}
Xcode 10.2.1 was showing me the error
Implicit instantiation of undefined template 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >'.
#include <iostream>
#include <vector>
int main(int argc, const char * argv[]) {
std::vector<std::string> listRestaurants;
....
return 0;
}
Fixed my issue.
If adding std:: is not the issue for you, then check if you have #include <vector>. That fixed the issue for me.
Didn't realize that #include <vector> is required. I thought it was part of standard library template; I ran this code in VSCODE IDE and it worked fine for me
#include <iostream>
#include <vector>
using namespace std;
int main()
{
uint_least8_t i; // trying to be conscious of the size of the int
vector<int> vect;
for(i = 0; i < 5; ++i)
{
vect.push_back(i);
}
for(auto i : vect)
{
cout << i << endl;
}
return 0;
}
From the comments:
The std namespace houses both of those templates. Change vector to std::vector, and string to std::string. – WhozCraig
the vector and string were placed in the namespace std
using namespace std;
Error: multiple definition of `GameKey::getGameKeywords()'
GameKey.cpp and .h cause error, while ExitKey.cpp and .h are essentially the exact same class and header but do not produce an error.
(I know the whole thing about using namespace std)
//Function Declarations
#ifndef GAMEKEY_H
#define GAMEKEY_H
// C++ libraries
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
class GameKey
{
private:
string keyString;
string lineData;
public:
// Default constructor
GameKey();
// Deconstructor
~GameKey();
// Get keywords
string getGameKeywords();
};
#endif
GameKey.cpp
//Function Definitions
#include "GameKey.h"
// Constructor
GameKey::GameKey()
{
}
// Deconstructor
GameKey::~GameKey()
{
}
// Get keywords
string GameKey::getGameKeywords()
{
ifstream infile;
infile.open("GameKey.txt");
while (getline(infile, lineData))
{
keyString.append(lineData);
keyString.append("\n");
}
infile.close();
return keyString;
}
ExitKey.h
//Function Declarations
#ifndef EXITKEY_H
#define EXITKEY_H
// C++ libraries
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
class ExitKey
{
private:
string keyString;
string lineData;
public:
// Default constructor
ExitKey();
// Deconstructor
~ExitKey();
// Get keywords
string getExitKeywords();
};
#endif
ExitKey.cpp
//Function Definitions
#include "ExitKey.h"
// Constructor
ExitKey::ExitKey()
{
}
// Deconstructor
ExitKey::~ExitKey()
{
}
// Get keywords
string ExitKey::getExitKeywords()
{
ifstream infile;
infile.open("ExitKey.txt");
while (getline(infile, lineData))
{
keyString.append(lineData);
keyString.append("\n");
}
infile.close();
return keyString;
}
Thanks for any help!
I think you probably include GameKey.cpp instead of GameKey.h elsewhere
I am not certain as the command used for compilation is not posted.
One possibility is repeating the file names in your compilation command could also lead to this error.
for example :-
g++ ExitKey.cpp GameKey.cpp GameKey.cpp main.cpp -o main