vector<string> declaration in c++ - c++

I'm very new to c++, so I'm sorry, if this solution is very trivial. I was searching this site for a solution, but didn't find something.
I need to do some thermodynamic calculus and therefore initialice my class with some information. This code breaks when I compile it because of my components_i.
I'm looking for a way to pass a vector to my class or an other way to provide my calculus in the class with the information about the mixture I want to simulate.
h-file:
#ifndef BASE_H
#define BASE_H
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Base
{
public:
vector<string> components_i;
int nComp,nPhase;
long double T,P,N_p_i,x_p_i;
Base( vector<string>,int,int,long double,long double,long double );
};
#endif
and as cpp:
#include "Base.h"
Base::Base( vector<string> components_i,int nComp, int nPhase, long double T, long double P, long double N_p_i ) {
Base::components_i = components_i //e.g. H2O and Ethanol
Base::nComp = nComp;
Base::nPhase = nPhase;
Base::T = T;
Base::P = P;
Base::N_p_i = N_p_i;
}
This doesnt work at all, but I think it makes more clear, what I want to do
Any suggestions?

You did not declare your static member. Static members need to be declared explicitly outside of class declaration, add this:
vector<string> Base::components_i;
Full code should be something like:
#include <stdio.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Base
{
public:
static vector<string> components_i;
int nComp,nPhase;
long double T,P,N_p_i,x_p_i;
Base( vector<string>,int,int,long double,long double,long double );
};
vector<string> Base::components_i;
Base::Base(vector<string> components_i,int nComp, int nPhase, long double T, long double P, long double N_p_i ) {
Base::components_i = components_i; //e.g. H2O and Ethanol
Base::nComp = nComp;
Base::nPhase = nPhase;
Base::T = T;
Base::P = P;
Base::N_p_i = N_p_i;
};
int main() {
vector<string> vec;
vec.push_back(string("hello"));
Base base = Base(vec, 1, 1, 2.0, 2.0, 3.0);
printf("%s", Base::components_i[0].c_str());
}
Here is a working example:
http://cpp.sh/9xkfk

Related

Class Composition Constructor d Must Explicitly initialize the reference member

I am really stuck with the problem here.
So basically I have a Stock class and a BuyOrder class.The BuyOrder class takes the Stock class as a member so it knows which stock to buy. When the stock is created, it takes in a company name and initializes with random prices. Then I can create a buy order takes in bidPrice,bidQuantity,Stock arguments.
In the main I want to create an array of BuyOrder to store the orders. Then every time the user created an order, the order can be stored.
My first approach is to declare BuyOrder buyList[100]. This way every time the user created an order, I can use the set functions to set each buyList[i] and then increment i. However, when I declare BuyOrder buyList[100]in main. It says No matching constructor for initialization of 'BuyStock buyList[100]' I went back to BuyOrder.cpp try to add a default constructor BuyOrder::BuyOrder(){} then it shows the error:Constructor for 'BuyOder'must explicitly initialize the reference member 'buyStock'.
I don't know how to proceed from here. Very much appreciated.
Below is part of BuyOrder.cpp since it way too long and the rest part are just function definitions.
BuyOrder::BuyOrder{double price, int quantity, Stock &s)
:buyPrice{price},
buyQuantity{quantity},
buyStock{s}{}
void BuyOrder::setBuyStock(Stock stock){
buyStock = stock;
}
void BuyOrder::setBuyOrderPrice(double price) {
buyPrice = price;
}
void BuyOrder::setBuyOrderQuantity(int quantity) {
buyQuantity = quantity;
Below is BuyOrder.h
#ifndef BUYORDER_H
#define BUYORDER_H
#include <ctime>
#include<iostream>
#include "Stock.h"
class BuyOrder {
friend void getCurrentTime();
public:
BuyOrder(double , int , Stock & );
void setBuyStock(Stock);
void setBuyOrderPrice(double price);
void setBuyOrderQuantity(int quantity);
double getBuyOrderPrice();
int getBuyOrderQuantity();
void placeBuyOrder();
void checkExcute();
void modifyBuyOrder();
void cancelBuyOrder();
double getHighestBidPrice();
int getHighestBidPriceQuantity();
double getLowestBidPrice();
int getLowestBidPriceQuantity();
private:
double buyPrice;
int buyQuantity;
bool excute = false;
Stock &buyStock;
time_t t;
};
#endif
This is the Stock.h
#ifndef STOCK_H
#define STOCK_H
#include <vector>
#include <string>
using namespace std;
class Stock {
friend void getCurrentTime();
public:
Stock();
Stock(string nameOfCompany);
int getBidTerms();
int getAskTerms();
void printStockInfo();
void initialize();
void sortBid();
void sortAsk();
string nameOfCompany;
string currentTime;
vector<double> askPrice;
vector<int> askQuantity;
vector<double> bidPrice;
vector<int> bidQuantity;
int bidTerm;
int askTerm;
};
#endif
and this is Stock.cpp excluding the long function definitions
#include "Stock.h"
#include <iostream>
#include <vector>
#include <time.h>
#include <iomanip>
#include <ctime>
#include <random>
#pragma warning(disable : 4996)
using namespace std;
void getCurrentTime() {
time_t rawTime;
struct tm * timeinfo;
time(&rawTime);
timeinfo = localtime(&rawTime);
cout << asctime(timeinfo);
}
Stock::Stock(){}
Stock::Stock(string companyName) :nameOfCompany{ companyName } {
initialize();
sortBid();
sortAsk();
}
Because your BuyOrder class (which you don't show us, and should) contains a reference member (Stock &buyStock;), you have to set that reference to something within any constructor for the class. This means that you can't normally use a default constructor, as that does not initialize the reference.
Possible solutions include changing buyStock to not be a reference, changing it to be a pointer (which can be nulled out for those cases where it doesn't represent an order yet), or changing containers from a fixed size array to something that can be resized, like a vector.

Constructor of a children class that have an array that contains objects of another class

Dialog.h
#include "WBasic.h"
#include "WButton.h"
#include "WData.h"
#ifndef WDIALOG_H_INCLUDED
#define WDIALOG_H_INCLUDED
class WDialog : public WBasic
{
private:
WButton wB;
WData wD;
public:
//Constructor
WDialog(const int& e = 0, const WButton& = WButton(0,0), const WData& = WData(0,0,0));
~WDialog();
};
#endif // WDIALOG_H_INCLUDED
Dialog.cpp
#include <iostream>
#include "WDialog.h"
WDialog::WDialog(const int& e, const WButton& WBUTTON, const WData& WDATA) :
WBasic(e), wB(WBUTTON), wD(WDATA)
{
}
The code above works great, however I'm trying to make "WButton wB" a vector changing it to"WButton wB[3];"
class WDialog : public WBasic
{
private:
WButton wB[3];
WData wD;
};
But then I've no idea how deal with the Constructor.
You can use vector to solve this problem.
I have written a small example below.
#include <iostream>
#include <vector>
using namespace std;
class A{
};
class B{
public:
B():vec (4,A())
{
}
private :
vector<A> vec;
};
int main() {
// your code goes here
B obj();
return 0;
}
You can observe how I have initialized vector vec with three class A object.
In my opinion if you can (your compiler support C++11) prefer std::array
#include <array>
std::array<WButton, 3> wB;
Then in your contructor use an initializer list:
WBasic(e),
wB{WButton(...), WButton(...), WButton(...)},
wD(WDATA)

No predefined constructor existing C++

I have been spending hours on this but I can't seem to find a solution to this problem.
I have am working with two header files, one is Load.h and one is Source.h.
This is my load.h:
#ifndef LOAD_H
#define LOAD_H
#include <string>
#include "Complexnumbersfrompreviousweek.h"
#include "Otherfunctionsfrompreviousweek.h"
#include "Source.h"
class Load : public Source //I'm doing this to inherit the vs term
{
private:
double load;
double vload;
double ApparentP;
public:
Load (double, double, double, double);
double Calcvload (double, double, double, double);
};
#endif LOAD_H
This is my Source.h:
#ifndef SOURCE_H
#define SOURCE_H
#include <string>
#include "Complexnumbersfrompreviousweek.h"
#include "Otherfunctionsfrompreviousweek.h"
class Source {
public:
double vs;
Source(double);
double Ret(double);
};
#endif SOURCE_H
And this is my second .cpp file:
#include "Line.h"
#include "Load.h"
#include "Source.h"
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
Source::Source(double VoltageS)
{
VoltageS = vs;
};
double Source::Ret(double vs)
{
return vs;
}
Load::Load(double VoltageS, double Sl, double Z, double Vl)//Constructor
{
Z = load;
Sl = ApparentP;
Vl = vload;
VoltageS = vs;
};
The error I get is error C2512: 'Source' no predefined appropriate constructor available.
This is what I am doing in my main():
Source Sorgente(VoltageS);
Load loadimpedance(VoltageS, Sl, Z, Vl);
So basically I am creating the "Sorgente" object using VoltageS as an argument (selected by the user, I didn't put that bit of code in) and I am trying to assign it to Vs in order to use it in the constructor for the Load afterwards...
Thank you in advance for your help!
Since Load inherits from Source, it has to construct the Source base in its constructor:
Load::Load(double VoltageS, double Sl, double Z, double Vl)//Constructor
{
Since you don't explicitly specify one, the compiler will automatically insert the default:
Load::Load(double VoltageS, double Sl, double Z, double Vl)//Constructor
: Source() // implicitly inserted by compiler
{
But that constructor doesn't exist - hence the error. To fix this, you need to explicitly call the correct constructor:
Load::Load(double VoltageS, double Sl, double Z, double Vl)//Constructor
: Source(VoltageS) // explicitly construct the base
{
Unrelatedly, in your Source constructor you are assigning the wrong element:
Source::Source(double VoltageS)
{
VoltageS = vs; // you are assigning to the temporary instead of your member
}
That should be:
Source::Source(double VoltageS)
: vs(VoltageS)
{ }

Two errors using class and header

I get the error class redefinition
Also the error ID is not a member of class process
and missing semi colon before ID
I tried char* instead of string.. not working also
Any help? I seem to be missing something
Thanks in advance
process.h file
#ifndef PROCESS_H
#define PROCESS_H
class process
{
public:
string ID;
int run_time;
int arrival_time;
process();
int get_start_time();
int get_finish_time(int start, int run);
int get_TA_time(int finish, int start);
float get_weighted_TA_time();
};
#endif
process.cpp file
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <sstream>
#include "process.h"
using namespace std;
class process
{
public:
process:: process() {};
int process:: get_start_time()
{
int x;
return x;
}
int process:: get_finish_time(int start, int run)
{
int x;
return x= start +run;
}
int process:: get_TA_time(int finish, int start)
{
int x;
return x= finish - start;
}
float process:: get_weighted_TA_time()
{};
};
Problem 1 Class refinition
In cpp file you don't need to write class process. It should be
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <sstream>
#include "process.h"
using namespace std;
process:: process() {};
int process:: get_start_time()
{
int x;
return x;
}
int process:: get_finish_time(int start, int run)
{
int x;
return x= start +run;
}
int process:: get_TA_time(int finish, int start)
{
int x;
return x= finish - start;
}
float process:: get_weighted_TA_time()
{};
In header file you have given required declarations. In cpp, you need to give the definitions. But because you are outside the class scope now, you must give the fully qualified name of members which you are already doing. (for ex: int process:: get_finish_time(int start, int run))
If you again use class process, compiler has no hint that you intend the same class and not want a new class causing the class redifinition error. In C++, it is not allowed to make a class partially and complete the rest of it somewhere else. (Not to be cofused with inheritance)
Problem 2: string issue
Add #include <string> in your header file and use std::string or add line using std::string
There are a few issues. One is this:
process.h needs to #include <string>. Once included, prepend all string variables with std::.
#ifndef PROCESS_H
#define PROCESS_H
#include <string>
class process
{
public:
std::string ID;
int run_time;
int arrival_time;
process();
int get_start_time();
int get_finish_time(int start, int run);
int get_TA_time(int finish, int start);
float get_weighted_TA_time();
};
#endif
The problem with your original code is that you're:
Relying that some outside module included <string> before process.h is included.
Relying that some outside module stated using namespace std; before including process.h.
None of those can be guaranteed.

no instance of overloaded function matches the argument list c++

I'm a little confused by this error that I'm having (I'm using VS2012).
Here's my code:
RecipeBook.h:
#ifndef RECIPEBOOK_H
#define RECIPEBOOK_H
#include "SingleRecipe.h"
using namespace std;
class RecipeBook
{
private:
vector<SingleRecipe> *recipe;
SingleRecipe *one;
public:
RecipeBook(vector<SingleRecipe> *recipe);
void addRecipe(SingleRecipe *one);
bool removeRecipe(string name);
vector <SingleRecipe> *returnListOfRecipes(double time);
};
#endif
SingleRecipe.h:
#ifndef SINGLERECIPE_H
#define SINGLERECIPE_H
#include <string>
#include <vector>
using namespace std;
class SingleRecipe
{
private:
string name;
vector<string> ingredients;
vector<string> method;
int numOfServing;
double time;
public:
SingleRecipe(string name, vector<string> ingredients, vector<string> method, int numOfServing, double time);
string getName();
void setName();
int getNumOfServing();
void setNumOfServing();
double getTime();
void setTime();
string toString();
};
#endif
BookAndRecipe.cpp:
#include "RecipeBook.h"
#include "SingleRecipe.h"
#include <sstream>
#include <math.h>
using namespace std;
vector <SingleRecipe> *RecipeBook::returnListOfRecipes(double time)
{
vector<SingleRecipe> *two;
for (int i = 0; i = recipe->size(); i++)
{
if (recipe[i].data()->getTime < time)
{
*two->push_back(recipe[i].pop_back());
}
}
return NULL;
}
Over at returnListOfRecipes() I get this error:
no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=SingleRecipe, _Alloc=std::allocator<SingleRecipe>]" matches the argument list
argument types are: (void)
object type is: std::vector<SingleRecipe, std::allocator<SingleRecipe>> c:\Users\Ventus\Documents\Visual Studio 2012\Projects\Recipe\Recipe\BookAndRecipe.cpp 83 8
I suspect it might have something wrong with my for loop, but I'm not very experienced, so I might be doing something very wrong here.
I appreciate all help that's given!
pop_back() doesn't return a value, it just drops the last element of the container. You probably want:
*two->push_back(recipe[i].back());
recipe[i].pop_back();