In a program the following struct is defined in a header file:
\\structs.h
#include <vector>
using std::vector;
using namespace std;
struct cell
{
double x;
vector<int> nn;
};
In a separate source file I define the function:
\\functions.cpp
# define _CRT_SECURE_NO_DEPRECATE
# include <stdio.h>
# include <iostream>
# include <math.h>
# include <vector>
# include "structs.h"
using namespace std;
void initial_position(vector<cell>& cluster, int n)
{
cell tmp;
for (int i = 0; i < n; i++)
{
tmp.x = 1;
cluster.push_back(tmp);
}
}
with a header file:
//functions.h
# include <vector>
using std::vector;
void initial_position(vector<cell>& cluster, int n);
I wish to call this function in the main script:
//main.cpp
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <vector>
#include "functions.h"
#include "structs.h"
using namespace std;
int main()
{
vector <cell> cluster;
int n = 100;
initial_position(cluster,n);
return 0;
}
but get the following errors:
functions.h(4): error C2065: 'cell': undeclared identifier
functions.h(4): error C2923: 'std::vector': 'cell' is not a valid template type argument for parameter '_Ty'
functions.h(4): error C3203: 'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type
main.cpp(14): error C2664: 'void initial_position(std::vector &)': cannot convert argument 1 from 'std::vector>' to 'std::vector &'
What is the source of the errors? it all seems to be well defined.
Put
#include "structs.h"
into functions.h and protect both structs.h and functions.h with include-guards, e.g.
#pragma once
if available.
add
#include "structs.h"
into functions.h since in functions.h compiler doesn't know what cell is.
Just swap
#include "functions.h"
#include "structs.h"
with
#include "structs.h"
#include "functions.h"
Since cell is declared in structs.h and is needed in functions.h
Or better yet, include structs.h in functions.h
You should also not place using namespace std in a header, that is bad practice. It can cause some nasty hard to find bugs, see e.g. Why is "using namespace std" considered bad practice?
Related
I included all the needed headers (windows.h, winuser.h), but the compiler keeps complaining about "GetDpiForWindow" or "AdjustWindowRectExForDpi" not being defined.
Code example:
#include <iostream>
#include <windows.h>
#include <winuser.h>
using namespace std;
int main()
{
int dpi = GetDpiForWindow(GetConsoleWindow());
return 0;
}
Error:
error: 'GetDpiForWindow' was not declared in this scope; did you mean
'GetTopWindow'?
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 am getting errors with the following code. The errors are incomplete type is not allowed and use of undefined type 'mGame'.
header.h:
//--Libraries
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//--Classes
class mGame;
Game.cc:
#include "header.h"
class mGame
{
private:
public:
bool intro();
};
Intro.cc:
#include "header.h"
bool mGame::intro() //--Line 3
{
printf("|-----------------------------|\n");
printf("\n Welcome to the Guessing Game!\n");
printf("\n|-----------------------------|\n");
return false;
}
The errors are both on line 3 of intro.cc. I tried finding a solution, but I couldn't for what I am doing.
header.h doesn't know any definitions of game.cc, you tell header.h only, that there is a class mGame. rename game.cc to game.h and include it into header.h and delete the line "class mGame;"
To be able to use mGame from Intro.cc, you have to move the class declaration into header.h (or into some other header file that you include from Intro.cc).
Having a forward declaration in header.h is not enough (that's what is meant by "incomplete type is not allowed").
I just have an header file and and an .cpp file i am just passing an value to function but it gives me an error
main.c
#include "me.h"
#include <iostream>
#include <sstream>
#include <string.h>
using namespace std;
int main()
{
me("http");
}
me.h
#ifndef ME_H_
#define ME_H_
#include <string.h>
class me {
public:
me(std::string u);
virtual ~me();
};
#endif /* ME_H_ */
me.cpp
#include "me.h"
#include <iostream>
#include <string.h>
using namespace std;
me::me(std::string u) {
// TODO Auto-generated constructor stub
cout << "help";
}
me::~me() {
// TODO Auto-generated destructor stub
}
I am getting an error
In file included from ../src/me.cpp:8:
../src/me.h:13: error: expected ‘)’ before ‘u’
../src/me.cpp:12: error: prototype for ‘me::me(std::string)’ does not match any in class ‘me’
../src/me.h:11: error: candidates are: me::me(const me&)
../src/me.h:11: error: me::me()
make: *** [src/me.o] Error 1
#include <string> instead of #include <string.h>
string.h is the C string header, accessible in C++ as <cstring>
<string> is the C++ header that defines std::string
you want #include <string> instead of #include <string.h>