Source File:
#include "WeatherForecaster.h"
#include <iostream>
using namespace std;
WeatherForecaster::WeatherForecaster(string d, string fd, int h, int l, int hum,int avgw, string avgwd, int maxw, string maxwd, double p){
string day=d;
string forecastDay=fd;
int highTemp=h;
int lowTemp =l;
int humidity=hum;
int avgWind= avgw;
string avgWindDir=avgwd;
int maxWind=maxw;
string maxWindDir= maxwd;
double recip=p;
}
WeatherForecaster::WeatherForecaster(){
//dtor
}
void AddDaytoData(ForecastDay){
}
Header File:
#ifndef WEATHERFORECASTER_H
#define WEATHERFORECASTER_H
#include <iostream>
using namespace std;
struct ForecastDay{
std::string day;
std::string forecastDay;
int highTemp;
int lowTemp;
int humidity;
int avgWind;
std::string avgWindDir;
int maxWind;
std::string maxWindDir;
double precip;
};
class WeatherForecaster
{
public:
WeatherForecaster(string, string, int, int,
int,int, string, int, string , double );
WeatherForecaster();
void addDayToData(ForecastDay);
void printDaysInData(); //prints the unique dates in the data
void printForecastForDay(std::string);
void printFourDayForecast(std::string);
double calculateTotalPrecipitation();
void printLastDayItRained();
void printLastDayAboveTemperature(int);
void printTemperatureForecastDifference(std::string);
void printPredictedVsActualRainfall(int);
std::string getFirstDayInData();
std::string getLastDayInData();
protected:
private:
int arrayLength;
int index;
ForecastDay yearData[984]; //data for each day
};
#endif // WEATHERFORECASTER_H
ERROR: My error happens when I declare try to reach a function in my source file after declaring an instance of the class Weather Function.
ex:
WeatherForecaster wf;
wf.AddDayToData();
//undefined reference to 'WeatherForecaster::AddDaytoData'
I am not entirely sure where the lack of referencing is happening, also I have the header included in main, as well as all other relevant additions.
Edit: I added a function as an example
In your cpp file you need to scope your member function definition
WeatherForecaster::AddDayToData(ForecastDay)
instead of
AddDayToData(ForecastDay)
Also in your call you must pass an argument since it's not optional
Related
I am new to C++ and I am having trouble with class and header files. I am trying to create a constructor to accept various pokemon stats such as strings and integers. I used to code in java and constructors were fairly simple to assign.
Pokemons.h
#ifndef POKEMONS_H
#define POKEMONS_H
#include <string>
#include <iostream>
using namespace std;
class Pokemons {
public:
Pokemons();
};
#endif /* POKEMONS_H */
Pokemons.cpp
#include "Pokemons.h"
#include <string>
using namespace std;
string pokemonName;
string pokemonType1;
string pokemonType2;
int pokemonHP;
int pokemonAttack;
int pokemonDefence;
int pokemonSPAttack;
int pokemonSPDefence;
int pokemonSpeed;
Pokemons::Pokemons(string nm, string tp1, string tp2, int hp, int atk,
int def, int satk, int sdef, int spd) {
pokemonName = nm;
pokemonType1 = tp1;
pokemonType2 = tp2;
pokemonHP = hp;
pokemonAttack = atk;
pokemonDefence = def;
pokemonSPAttack = satk;
pokemonSPDefence = sdef;
pokemonSpeed = spd;
}
main.cpp
#include <iostream>
#include "Pokemons.h"
int main(){
Pokemons p001;
p001.Pokemons("Bulbasaur", "Grass", "None", 31,23,45,43,45,12);
return 0;
}
I am getting the following errors :
Pokemons.cpp:32:9: error: prototype for 'Pokemons::Pokemons(std::string, std::string, std::string, int, int, int, int, int, int)' does not match any in class 'Pokemons'
Pokemons::Pokemons(string nm, string tp1, string tp2, int hp, int atk, int def, int satk, int sdef, int spd) {
In file included from Pokemons.cpp:14:0:
Pokemons.h:21:7: error: candidates are: constexpr Pokemons::Pokemons(Pokemons&&)
class Pokemons {
Pokemons.h:21:7: error: constexpr Pokemons::Pokemons(const Pokemons&)
Pokemons.cpp:30:9: error: Pokemons::Pokemons()
Pokemons::Pokemons(){}
There are three problems here. First, your constructor is declared as Pokemons();, taking zero arguments, but you have a constructor defined as taking many arguments, and so their signatures don't match, and ultimately, because of function overloading in C++, they refer to different functions. Try declaring the constructor as follows in your header file:
class Pokemons {
public:
Pokemons(string nm, string tp1, string tp2, int hp, int atk,
int def, int satk, int sdef, int spd);
};
Now both the definition and declaration should refer to the same function.
The second problem is here:
Pokemons p001;
This implicitly calls the constructor with no arguments. It's important to understand that many functions get called in C++ even though they haven't been explicitly named. To fix this, you should initialize p001 as follows:
Pokemons p001("Bulbasaur", "Grass", "None", 31,23,45,43,45,12);
You should also remove p001.Pokemons("Bulbasaur", "Grass", "None", 31,23,45,43,45,12); on the following line. Now, the compiler can match this call to your constructor taking many arguments.
The third problem for now is that pokemonName all the way through pokemonSpeed are defined outside the Pokemons class, in global scope. This has a similar effect to making a member static in Java. These should be placed inside your class definition, to make them instance members:
class Pokemons {
public:
Pokemons(string nm, string tp1, string tp2, int hp, int atk,
int def, int satk, int sdef, int spd);
private:
string pokemonName;
string pokemonType1;
string pokemonType2;
int pokemonHP;
int pokemonAttack;
int pokemonDefence;
int pokemonSPAttack;
int pokemonSPDefence;
int pokemonSpeed;
};
**On my main i can't add a note on my new Object of the Class Trabalho
ass.add_nota(num);
**
There is a error on my compilation.
My "Trabalho.h" code:
#include <string>
#include <vector>
#include <iostream>
//#include "Enunciado.h"
//#include "Pessoa.h"
using namespace std;
class Estudante;
class Enunciado;
template <class T>
class Trabalho{
static int id_auxiliar;
string texto;
int ano;
int id;
vector<float> calif;
T* Enun;
vector<Estudante*> estudantes;
vector<Enunciado*> enunciados;
public:
Trabalho();
Trabalho(string texto, vector<Estudante*> est, T* en, int ano);
~Trabalho();
void set_texto(string texto);
string get_texto();
void add_nota(float nota);
void add_enun(Enunciado* en){Enun = en;};
int get_id(){return id;};
int get_ano() {return ano;};
void reutilizar(int id_enun);
vector<float> get_calif() {return calif;};
vector<Estudante*> get_estudantes() {return estudantes;};
Enunciado* get_enunciado() {return Enun;};
};
#endif
And my main code:
int main(int argc, char const *argv[]) {
int n;
int m;
Pesquisa ah();
float num = 1.1;
Trabalho<Pesquisa> ass();
Trabalho<Pesquisa>* tass = new Trabalho<Pesquisa>();
ass.add_nota(num);
tass->add_nota(num);
#ifndef ENUNCIADO_H_
#define ENUNCIADO_H_
#include "trabalho.h"
#include "Pessoa.h"
#include <string>
using namespace std;
class Enunciado
{
static unsigned int id_auxiliar;
const unsigned int id;
string titulo;
string descricao;
vector<int> anos_utilizados;
static unsigned int max_util;
public:
Enunciado(string titulo, string descricao);
virtual ~Enunciado();
int get_id(){return id;};
void set_titulo(string titulo);
string get_titulo();
void set_descricao(string descricao);
string get_descricao();
vector<int> get_anos_utilizados();
void mod_max_util(int a);
};
class Pesquisa: public Enunciado{
vector<string> ref;
public:
Pesquisa(string tit, string des, vector<string> refe);
};
class Analise: public Enunciado{
vector<string> repositorios;
public:
Analise(string tit, string des, vector<string> repos);
};
class Desenvolvimento: public Enunciado{
public:
Desenvolvimento(string tit, string des);
};
#endif
Both ways when i create a new Trabalho when i define my type (pesquisa is a class type on #include "Enunciado.h".
This is the two erros that appears:
"Description Resource Path Location Type
request for member 'add_nota' in 'ass', which is of non-class type 'Trabalho()' Test.cpp /Trabalho1/src line 42 C/C++ Problem
"
And:
Description Resource Path Location Type
Method 'add_nota' could not be resolved Test.cpp /Trabalho1/src line 42 Semantic Error
Can anyone help?
Thank you !
Your error is in trying to call the default constructor as
Pesquisa ah();
or
Trabalho<Pesquisa> ass();
Unfortunately, C++ is very misleading in this and it would declare your variable ass of type Trabalho<Pesquisa>(), which means "a function of zero arguments returning Trabalho<Pesquisa>" and that's exactly that the compiler error says: a function type is not a class type and as such does not have the member add_nota. Indeed, it does look exactly like a function declaration, if you look at it that way:
int main();
^ ^ ^
type arguments
name
It's a very common mistake, especially for those coming from a Java background. But it can easily catch a C++ programmer off guard as well. More information can be found here or here or here, you can see that the same error message has perplexed a good many people.
If you have a compiler conforming to the C++11 language revision, try replacing all those occurrences by
Trabalho<Pesquisa> ass{};
If not, just leave
Trabalho<Pesquisa> ass;
Unlike in Java, this does not mean that the variable will stay uninitialized. It's the C++ way to call a default (zero-argument) constructor.
error: "prototype for WeatherForecaster::WeatherForecaster(std::all of the variables) does not match any class in WeatherForecaster"
I'm out of ideas on how to avoid this. My main code has nothing to do with the error btw.
MOST RECENT ERROR, THE REST FIXED. I now get the error in main "no matching function to call to WeatherForecast::WeatherForecast()". After I create the variable wf WeatherForecast.
Source:
#include "WeatherForecaster.h" //header being included
#include<iostream>
using namespace std;
//error comes here
WeatherForecaster::WeatherForecaster(string d, string fd, int h, int l,
int hum,int avgw, string avgwd, int maxw, string maxwd, double p)
{
string day=d;
string forecastDay=fd;
int highTemp=h;
int lowTemp =l;
int humidity=hum;
int avgWind= avgw;
string avgWindDir=avgwd;
int maxWind=maxw;
string maxWindDir= maxwd;
double recip=p;
}
WeatherForecaster::~WeatherForecaster(){
//dtor
};//end of block of source code
Header: I am making such a simple mistake, I'm just not sure what it exactly is.
#ifndef WEATHERFORECASTER_H
#define WEATHERFORECASTER_H
#include <iostream>
using namespace std;
//does my code have a problem with how it interacts with this struct?
struct ForecastDay{
std::string day;
std::string forecastDay;
int highTemp;
int lowTemp;
int humidity;
int avgWind;
std::string avgWindDir;
int maxWind;
std::string maxWindDir;
double precip;
};
class WeatherForecaster
{
public://most recent error ") expected before 'd'"
WeatherForecaster(string d, string fd, int h, int l,
int hum,int avgw, string avgwd, int maxw, string maxwd, double p);
~WeatherForecaster();
void addDayToData(ForecastDay);
void printDaysInData();
void printForecastForDay(std::string);
void printFourDayForecast(std::string);
double calculateTotalPrecipitation();
void printLastDayItRained();
void printLastDayAboveTemperature(int); //argument is the
temperature
void printTemperatureForecastDifference(std::string);
void printPredictedVsActualRainfall(int);
std::string getFirstDayInData();
std::string getLastDayInData();
protected:
private:
int arrayLength;
int index;
ForecastDay yearData[984];
};
#endif // WEATHERFORECASTER_H
Where is your declaration of constructor taking these parameter (string d, string fd, int h, int l, int hum,int avgw, string avgwd, int maxw, string maxwd, double p) in the header?
It is exactly as the error message states: you need to add a prototype for "WeatherForecaster(string d, string fd, int h, int l,int hum,int avgw, string avgwd, int maxw, string maxwd, double p)" in your WeatherForecaster class.
I'm using Visual Studio 2010, and I'm receiving this error despite the fact that I included the appropriate header file, and the header file is in the same folder as the cpp file.
Here is the header file, msg.h:
#pragma once
#ifndef _MYMSG_
#define _MYMSG_
using namespace std;
class Message{
public:
int type; // type: 0: request, 1: reply
int rid; // request id
int opid; // operation id
int arg1; // argument for operations
double arg2; // argument for operations
Message();
Message (int t, int r, int o, int a1, double a2);
char * marshal( int &n); // the result is a string of bytes, the lenghth of which is returned to n
void unmarshal(const char *, int n); //recover the content in the byte string to the fields
void print();
};
#endif
Here is msg.cpp:
#include "msg.h"
#include <iostream>
using namespace std;
Message::Message()
{
}
Message::Message (int t, int r, int o, int a1, double a2)
{
type = t;
rid = r;
opid = o;
arg1 = a1;
arg2 = a2;
}
The error is focused on Message::. Everything looks fine to me, but I'm a novice with bloodshot eyes at this point.
I am writing c++ code to implememt universal hash function, but it doesn't recognize the parameters, here is the code:
#include <math.h>
#include <cmath>
#include "hash.h"
#include <iostream>
using namespace std;
#include <string>
HASH::HASH(){
a=23;
b=88;
n=100;
p=997;
products=new product[n];
}
HASH::~HASH(){
delete []products;
}
HASH::HASH(int aa,int bb,int nn, int pp){
a=aa;
b=bb;
n=nn;
p=pp;
products=new product[n];
}
int HF(int key){
int index;
int h;
h=((((a*key)+b)% p)% n);
}
in HF function,it tells me that the parameters a,b,n,p are undefined. I defined them in the header file like this:
class HASH {
private:
int a,b,n,p;
product* products;
public:
HASH();
HASH(int aa,int bb, int nn, int pp);
~HASH();
bool insert(product s);
bool retrieve(int id,product &product);
bool updateName(int id);
bool updateCost(int id);
bool updateQuantity(int id);
bool remove(product &d);
int getNumberOfProducts();
};
int HF(int key);
First, never define a variable with a single char, that you will use a lot, or in class. If you wanna find it in code, it will be impossible - you will stop one each "a" char. Name it something readable.
Second, HF is not a member of class HASH, it can't see private variables of other classes.
Add to public part of HASH class
int HF(int key);
change procedure to
int HASH::HF(int key){ ...
and it will work
Add int HF to the public section of your class and in the file hash.cpp use int HASH:HF instead of int hash.
I noticed a small bug: your function HF doesn't return value - add return statement to HF function.