C++ Error 1 error C2664 passing array pointers - c++

I don't understand how fix this problem, tried many things but no solution. Help on it would be much appreciated. Thanks.
Error 1 error C2664: 'void showAllBuses(const Bus *[],int)' : cannot convert argument 1 from 'Bus **' to 'const Bus *[]'
void showAllBuses(const Bus* pBuses[], int numBus) {
for (int i = 0; i < numBus; i++) {
cout << "Bus no ." << numBus << " details: " << endl;
cout << "Number: " << pBuses[i]->getNumber() << endl;
cout << "Driver name: " << pBuses[i]->getDriver().getName() << endl;
cout << "Driver experience(years): " << pBuses[i]->getDriver().getYearsDriving() << endl;
}
}
void listBusesWithYearsDriving(const Bus* pBuses[], int numBus, int drivingYears) {
for (int i = 0; i < numBus; i++) {
if (pBuses[i]->getDriver().getYearsDriving() >= drivingYears) {
cout << "Bus number: " << pBuses[i]->getNumber() << endl;
cout << "Driver name: " << pBuses[i]->getDriver().getName() << endl;
cout << "Driver experience: " << pBuses[i]->getDriver().getYearsDriving() << endl;
}
}
}
void removeDriver(Bus* pBuses[], int busPos) {
pBuses[busPos]->removeDriver();
}
void main() {
const int ASIZE = 4;
int drivingYears = 0;
Bus* buses = new Bus[ASIZE];
for (int i = 0; i < ASIZE; i++) {
addNewBus(&buses[i]);
cout << "Bus " << i << ": " << &buses[i] << endl;
}
showAllBuses(&buses, ASIZE);
cout << "Please enter a minimum years of experience to look for: " << endl;
cin >> drivingYears;
listBusesWithYearsDriving(&buses, ASIZE, drivingYears);
removeDriver(&buses, 0);
showAllBuses(&buses, ASIZE);
delete[] buses;
buses = nullptr;
cout << "\n\n";
system("pause");
}

For any type T, T* can be implicitly converted to const T*, but T** cannot be implicitly converted to const T**. Allowing this would make it possible to violate constness(1).
A T ** can be converted to const T* const *, though. Since your function does not modify the array in any way, you can simply change its parameter like that:
void showAllBuses(const Bus* const * pBuses, int numBus) {
Bear in mind that in a function parameter declaration, * and the outermost [] are synonyms.
(1) Here's the code:
const int C = 42;
int I = -42;
int *p = &I;
int *pp = &p;
const int **cp = pp; // error here, but if it was allowed:
*cp = &C; // no problem, *cp is `const int *`, but it's also `p`!
*p = 0; // p is &C!

Related

Cannot Create a Lazy String and cannot access memory

After getting a segmentation fault error, the debugger gives me an error that says I cannot access memory at address 0x1. It also says cannot create a lazy string with address 0x0, and a non-zero length. Does anyone know how I can fix this?
Thanks!
#include <iostream>
#include <string>
#include "unorderedSet.h"
using namespace std;
int main()
{
int intArr[] = {12,23,4,7,12,9,10,6,23,11};
//Debugger points to here
string strArr[] = {"banana","apple","pear","grape",
"banana","fig","mango","orange","pear","guava"};
unorderedSet<int> intSet(20);
for (int i = 0; i < 10; i++)
{
intSet.insertEnd(intArr[i]);
}
unorderedSet<string> strSet(20);
for (int i = 0; i < 10; i++)
{
strSet.insertEnd(strArr[i]);
}
cout << "\nInteger Set: " << intSet << endl;
cout << "String Set: " << strSet << endl;
intSet.insertAt(5,30);
cout << "\nInsert At non-duplicate\nInteger Set: " << intSet << endl;
intSet.insertAt(5,11);
cout << "Insert At duplicate\nInteger Set: " << intSet << endl;
strSet.replaceAt(1,"pineapple");
cout << "\nReplace At non-duplicate\nString Set: " << strSet << endl;
strSet.replaceAt(3,"pear");
cout << "Replace At Duplicate\nString Set: " << strSet << endl;
int intArr1[] = {7,0,19,56,22,11,23,5};
//Debugger points to here
string strArr1[] = {"red","yellow","grape","banana","mango","orange","guava"};
unorderedSet<int> intSet1(20);
for (int i = 0; i < 8; i++)
{
intSet1.insertEnd(intArr1[i]);
}
unorderedSet<string> strSet1(20);
for (int i = 0; i < 7; i++)
{
strSet1.insertEnd(strArr1[i]);
}

error: overloaded function with no contextual type information usinf LEMON-graph-library

So I am implementing a Benders procedure using lazy constraints from GUROBI. As part of the subproblem procedure I need to process a graph using Breadth First Search, for which I am using LEMON. I am trying to implement use a visitor for the BFS search. However when I try to access the map of reached nodes using bfs.reached() I get a compiler (I suppose) error.
Here is the callback class implementation so far:
typedef ListDigraph::Node Node;
typedef ListDigraph::Arc Arc;
typedef ListDigraph::ArcMap<double> ArcDou;
typedef ListDigraph::NodeMap<double> NodeDou;
typedef ListDigraph::ArcMap<int> ArcInt;
typedef ListDigraph::ArcMap<bool> ArcBool;
typedef ListDigraph::NodeMap<int> NodeInt;
typedef ListDigraph::NodeIt NodeIt;
typedef ReverseDigraph<ListDigraph>::NodeIt NodeIt_rev;
typedef ListDigraph::ArcIt ArcIt;
class BendersSub: public GRBCallback
{
public:
const Instance_data &ins;
const vec4GRBVar &X;
const vec1GRBvar &u;
vec2GRBVar &p;
GRBModel &modelSubD;
BendersSub(const Instance_data &ins, const vec4GRBVar &X, const vec1GRBvar &u, vec2GRBVar &p, GRBModel &modelSubD)
:ins(ins), X(X), u(u), p(p),modelSubD(modelSubD){
//cout << "\n\tI:" << ins.I << " J:" << ins.J << " T:" << ins.T << " V:" << ins.V << flush;
}
protected:
void callback() {
try {
if (where == GRB_CB_MIPSOL) {
cout << "\n -- Entering Callback -- " << flush;
string var_name;
int I = ins.I , J = ins.I , T = ins.T , V = ins.V,i,j,k,t,v;
ListDigraph grafo;
Node x;
for( int t(0); t < ins.T+1; t++)
for ( int i(0); i < ins.I; i++)
x = grafo.addNode();
Arc arco;
int count( 0 );
for(t = 0; t < ins.T; ++t){
for(i = 0; i < ins.I; ++i){
for(j = 0; j < ins.J; ++j){
if ( i == j ){
arco = grafo.addArc(grafo.nodeFromId(i + ins.I * t),grafo.nodeFromId(j + (ins.I * (t+1))));
}else if ( (t + ins.tau.at(i).at(j)) <= ins.T-1 ){
arco = grafo.addArc(grafo.nodeFromId(i + ins.I * t),grafo.nodeFromId(j + ins.I * (t + ins.tau.at(i).at(j))));
}
else if ( (t + ins.tau.at(i).at(j)) > ins.T-1 ){
arco = grafo.addArc(grafo.nodeFromId(i + ins.I * t),grafo.nodeFromId(((ins.I*(ins.T+1)+1))-1-(ins.I)+j)) ;
}
}
}
}
NodeDou divergence (grafo);
ArcDou costo (grafo);
ArcBool filter (grafo, true);
NodeDou potential (grafo);
ReverseDigraph<ListDigraph> grafo_r(grafo);
using Visitor = Visitor_AcSup<NodeDou>;
for ( int v(0); v < V; v++){
double sum_sup(0.0);
Visitor visitor(grafo_r, divergence, sum_sup);
cout << "\nsum_sup: " << sum_sup << flush;
for (t = 0; t < T; t++){
for(i = 0; i < I ; i++){
if(divergence[grafo.nodeFromId(flat_ixt(ins,t,i))] < -EPS3){
BfsVisit<ReverseDigraph<ListDigraph>, Visitor> bfs(grafo_r,visitor);
bfs.run(grafo.nodeFromId(flat_ixt(ins,t,i)));
if( (sum_sup-fabs(divergence[grafo.nodeFromId(flat_ixt(ins,t,i))])) < -EPS4 ){
cout << "\nBuild Ray" << flush;
}
}
}
}
for (NodeIt_rev u(grafo_r); u != INVALID; ++u){
/*The error does not show when I comment this line.*/
cout << "\nId: " << grafo.id(u) << setw(10) << bfs.reached(u) << flush;
}
cout << "\nsum_sup: " << sum_sup << flush;
.
/* Remaining code */
.
.
}/*end_if v*/
//cout << "\n -- Exiting Callback -- " << flush;
}
}catch (GRBException e) {
cout << "Error number: " << e.getErrorCode() << endl;
cout << e.getMessage() << endl;
exit(EXIT_FAILURE);
}catch (const exception &exc){
cerr << "\nCallback - " << exc.what() << flush;
exit(EXIT_FAILURE);
}catch (...) {
cout << "Error during callback" << endl;
exit(EXIT_FAILURE);
}
}
};
Here is the (incomplete visitor) implementation.
template <typename DivMap >
class Visitor_AcSup : public BfsVisitor< const ReverseDigraph<ListDigraph> > {
public:
Visitor_AcSup(const ReverseDigraph<ListDigraph>& _graph, DivMap& _dirMap, double& _sum_sup)
: graph(_graph), dirMap(_dirMap), sum_sup(_sum_sup){
cout << "\n --Calling constructor of Visitor_AcSup -- " << endl;
sum_sup = 0.0;
}
void start (const Node &node){
//cout << "\nstart Node: " << graph.id(node) << flush;
sum_sup -= dirMap[node];
}
void reach (const Node &node){
//cout << "\nReach Node: " << graph.id(node) << flush;
}
void process (const Node &node){
//cout << "\nProcess Node: " << graph.id(node) << setw(5) << dirMap[node] << flush;
sum_sup += dirMap[node];
}
void discover(const Arc& arc) {
//cout << "\tDiscover Arc: " << graph.id(arc) << flush;
}
void examine(const Arc& arc) {
//cout << "\tExamine Arc: " << graph.id(arc) << flush;
}
private:
const ReverseDigraph<ListDigraph>& graph;
DivMap& dirMap;
double& sum_sup;
The error looks like this
functions.cpp:1845:59: error: overloaded function with no contextual type information
cout << "\nId: " << grafo.id(u) << setw(10) << bfs.reached(u) << flush;
^~~~~~~
Makefile:27: recipe for target 'VAP' failed
I am clueless about what is happening as the only error that comes up to my mind is conflict of keywords between namespaces
using namespace lemon;
using namespace lemon::concepts;
using namespace std;
but have found no resolution to this. I am a newbie in C++, so that I am asking you guys where this could possibly come from.

warning C4018: '<': signed/unsigned mismatch ONLY when I include Identical Functions

I am lost, when I ran my program last night it ran fine. When I added the power() function, suddenly lines which ran fine without adding the new code now trigger an error message:
warning C4018: '<': signed/unsigned mismatch
Why?
I feel I don't have the chops to explain this, so please follow the code below.
PLEASE RUN THE CODE WITH AND WITHOUT THIS power() FUNCTION. When run with the power() function, it makes error C4018 on the for loops in the exam() function! When run without the power() function, it runs FINE!!
#include <string>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
///the offending function///
double power(double base, int exponent)
{
double product;
//double base; int exponent;
std::cout << "enter a value for base: " << endl;
std::cin >> base;
std::cout << "enter exponenent: " << endl;
std::cin >> exponent;
double result = 1;
for (int i = 0; i < exponent; i++)
{
result = result * base;
//product = base exponent;
}
std::cout << product;
return product;
}
///after here, things run fine if you X out the aforementioned function! Wow!
void exam()
{
std::vector<int> scores;
int F;
F = 0; //string names;
std::cout << "enter exam scores int:" << endl;
//std::vector <string> names;
while (F != -1)
{
std::cout << "Enter a new exame score:" << endl;
std::cin >> F;
scores.push_back(F);
}
if (F == -1)
{
std::cout << "end of score entering" << endl;
}
for (int i = 0; i < scores.size(); i++)
{
std::cout << scores[i];
}
/*
while (i < scores.size())
{
std::cout << scores[i];
i++;
}
*/
std::cout << "yay you made this work!!!!!!!!!!!!!" << endl;
}
int multiply()
{
int a;
int b;
a = 8;
b = 4;
std::cout << a * b << endl;
std::cout << "f*** yeah" << endl << endl;
return 0;
}
void test()
{
std::vector<int> newvector;
int T;
std::cout << "enter vector variables: " << endl;
std::cin >> T;
newvector.push_back(T);
while (T != -1)
{
std::cout << "enter new vector variables T " << endl;
std::cin >> T;
newvector.push_back(T);
if (T == -1)
{
newvector.pop_back();
}
}
std::cout << "end of NewVector data inputs:" << endl;
for (int W = 0; W < newvector.size(); W++)
{
std::cout << newvector[W] << endl;
}
}
int main()
{
power(2, 3);
exam();
/*int result = multiply();
std::cout << "endl ;" << endl;
test();
system("pause"); */
multiply();
string name;
int a;
std::cout << "enter a variable for your name: " << endl;
std::getline(cin, name);
if (name == "aaron")
{
std::cout << " what a dumb name, aAron?" << endl;
}
else if (name == "todd")
{
std::cout << "what a dottly name, Todd" << endl;
}
else
{
std::cout << "your name = " << name << endl;
}
//std::vector <string>
std::vector<int> asdf;
std::cout << "enter an int for a" << endl;
std::cin >> a;
asdf.push_back(a);
while (a != -1)
{
std::cout << "enter another A: " << endl;
std::cin >> a;
asdf.push_back(a);
if (a == -1)
{
asdf.pop_back();
}
} //set var; checks if d<size(); if so, JUMP to std::cout<<; when finished with body, find after size(); == "d++", then refer back to declaration)
/*/ for(int G = 0; G<asdf.size(); G++)
{
std::cout << asdf[G] << endl;
} */
for (int i = 0; i < asdf.size(); i++)
{
std::cout << asdf[i] << "f*** it works!!!!!! " << endl;
}
for (int d = 0; d < asdf.size(); d++)
{ //htt ps://youtu.be/_1AwR-un4Hk?t=155
std::cout << asdf[d] << ", ";
}
std::cout << endl;
std::cout << std::accumulate(asdf.begin(), asdf.end(), 0);
//std::cout<<
system("pause");
return 0;
}
The presence of the power function should have no effect on this problem. Possibly you aren't seeing the warnings because without the power function the program does not compile.
In
for (int W = 0; W < newvector.size(); W++)
newvector.size() returns an unsigned integer. int W is a signed integer. You're getting exactly what you asked for.
You can change int W to vector<int>::size_type W (but the less verbose size_t W should also work) to make the error message go away, but this is an error where you would likely have to add more than 2 billion items to the vector to see manifest.
Solution:
for (vector<int>::size_type W = 0; W < newvector.size(); W++)
However this is a good place for a range-based for loop
for (const auto &val: newvector)
{
std::cout << val << endl;
}
By letting the compiler figure out all the sizes and types your life is much easier.
This is repeated several times throughout the code.
Re: WHEN RUN, It makes error C4018 -
YOU made that error (warning, actually), not "it".
That warning is reported by compiler, so you haven't run anything yet...
Your newly added function uses uninitialized variable product; in my version of Visual Studio it is an error.

Getting compilation error: "identifier 'objUser' is undefined" in my main() function

I'm trying to call the functions from my clsFamily class within the main, however, when I pass down the (&objUser) I get a message stating:
identifier 'objUser' is undefined
Edit: Sorry forgot to mention but I want a dynamic vector array that is manipulated based on the users choice.
Any help at all will be kindly appreciated as it is for an assignment that is due next Thursday xD
int UserChoice();
int UserChoice()
{
int iChoice = 0;
int iAdults = 0;
int iChildren = 0;
cout << "How many family members do you have? \n";
cin >> iChoice;
cout << "How many of these are adults? \n";
cin >> iAdults;
cout << "How many of these are children? \n";
cin >> iChildren;
return iChoice;
return iAdults;
return iChildren;
}
class clsUser
{
private:
string m_sName;
int m_iAge;
public:
void SetName(string);
string GetName();
void SetAge(int);
int GetAge();
clsUser();
~clsUser();
clsUser(string, int);
};
//This is to group the singular users into a group using a vector
class clsFamily
{
private:
vector <clsUser> objUser;
public:
void InputFamilyDetails(vector <clsUser> *objUser);
void OutputFamilyDetails(vector <clsUser> *objUser);
};
void clsFamily::InputFamilyDetails(vector <clsUser>* objUser)
{
string sName = "";
int iAge = 0;
for (int iCount = 0; iCount < objUser->size(); iCount++)
{
cout << "Please enter the name of family member " << iCount + 1 << "\n";
cin >> sName;
cout << "Please enter the age of family member " << iCount + 1 << "\n";
cin >> iAge;
objUser->at(iCount).SetName(sName);
objUser->at(iCount).SetAge(iAge);
}
}
//This is to allow the user to input the the details of the users from the vector
void clsFamily::OutputFamilyDetails(vector <clsUser>* objUser)
{
for (int iCount = 0; iCount < objUser->size(); iCount++)
{
cout << "The name of family member " << iCount + 1 << " is " << objUser->at(iCount).GetName() << " \n";
cout << "The age of family member " << iCount + 1 << " is " << objUser->at(iCount).GetAge()<< " \n";
}
}
int main()
{
clsFamily objFamily;
*//This is the area where the problem is occuring*
objFamily.InputFamilyDetails(&objUser);
objFamily.OutputFamilyDetails(&objUser);
}
clsFamily already has an objUser member. You shouldn't use it and not expect a parameter in InputFamiliyDetails and OutputFamilyDetails:
class clsFamily
{
private:
vector <clsUser> objUser; // Needs to be initialized with some size, though
public:
void InputFamilyDetails();
void OutputFamilyDetails();
};
void clsFamily::InputFamilyDetails()
{
string sName = "";
int iAge = 0;
for (int iCount = 0; iCount < objUser.size(); iCount++)
{
cout << "Please enter the name of family member " << iCount + 1 << "\n";
cin >> sName;
cout << "Please enter the age of family member " << iCount + 1 << "\n";
cin >> iAge;
objUser.at(iCount).SetName(sName);
objUser.at(iCount).SetAge(iAge);
}
}
void clsFamily::OutputFamilyDetails()
{
for (int iCount = 0; iCount < objUser.size(); iCount++)
{
cout << "The name of family member " << iCount + 1 << " is " << objUser.at(iCount).GetName() << " \n";
cout << "The age of family member " << iCount + 1 << " is " << objUser.at(iCount).GetAge()<< " \n";
}
}
int main()
{
clsFamily objFamily;
objFamily.InputFamilyDetails();
objFamily.OutputFamilyDetails();
}

Handing over std::vector to function with pointer

I have been searching on Google an in this forum for a while, but I could not find any answer or tip for my problem. Tutorials couldn't help me either...
I want to redistribute some points, stored in a vector p_org. (x-value is stored as double).
Therefore I have the function distribute, which is defined in maths.h
distribute_tanh(&p_org_temp,&p_new_temp,iz,spacing[0],spacing[1],l_rot[(kk+1)*iz-2],status);
The function distribute_tanh does look like this:
inline void distribute_tanh (std::vector<double> *p_org, std::vector<double> *p_new, const int n_points, double spacing_begin, double spacing_end, const double total_length, double status){
//if status == 0: FLAP, if status == 1: SLAT
std::cout << "spacing_begin: " << spacing_begin << " spacing_end: " << spacing_end << std::endl;
double s_begin = spacing_begin / total_length;
double s_end = spacing_end / total_length;
double A = sqrt(s_end/s_begin);
double B = 1 / (sqrt(s_end*s_begin)*n_points);
std::cout << "A: " << A << " B: " << B << std::endl;
std::vector<double> u (n_points);
std::vector<double> sn (n_points);
double dx;
double dy;
std::cout << "Control at the beginning: p_org: " << (p_org) << " p_new: " << (p_new) << " n_points: " << n_points << " s_begin: " << s_begin << " s_end: " << s_end << " total_length: " << total_length << std::endl;
//problem no. 1
for (int i=0;i<n_points;i++){
if (B > 1.001) {
if (B < 2.7829681) {
double Bq=B-1;
dy=sqrt(6*Bq)*(1-0.15*Bq+0.057321429*pow(Bq,2)-0.024907295*pow(Bq,3)+0.0077424461*pow(Bq,4)-0.0010794123*pow(Bq,5));
} else if (B > 2.7829681) {
double Bv=log(B);
double Bw=1/B-0.028527431;
dy=Bv+(1+1/Bv)*log(2*Bv)-0.02041793+0.24902722*Bw+1.9496443*pow(Bw,2)-2.6294547*pow(Bw,3)+8.56795911*pow(Bw,4);
}
u[i]=0.5+(tanh(dy*(i*(1.0/n_points)-0.5))/(2*tanh(dy/2)));
}
else if (B < 0.999) {
if (B < 0.26938972) {
dx=M_PI*(1-B+pow(B,2)-(1+(pow(M_PI,2))/6)*pow(B,3)+6.794732*pow(B,4)-13.205501*pow(B,5)+11.726095*pow(B,6));
} else if (B > 0.26938972) {
double Bq=1-B;
dx=sqrt(6*Bq)*(1+0.15*Bq+0.057321429*pow(Bq,2)+0.048774238*pow(Bq,3)-0.053337753*pow(Bq,4)+0.075845134*pow(Bq,5));
}
u[i]=0.5+(tan(dx*(i*(1.0/n_points)-0.5))/(2*tan(dx/2)));
}
else {
u[i]=i*(1.0/n_points)*(1+2*(B-1)*(i*(1.0/n_points)-0.5)*(1-i*(1.0/n_points)));
}
sn[i]=u[i]/(A+(1.0-A)*u[i]);
std::cout << "sn(i): " << sn[i] << std::endl;
std::cout << "p_org[n_points]: " << &p_org[n_points-1] << std::endl;
if(status==0){
//p_new[i]=p_org[0]+(total_length*sn[i]);
std::cout << "FLAP maths.h" << std::endl;
}
//Here is the problem no. 2
else if(status==1){
//p_new[i]=p_org[0]-(total_length*sn[i]);
std::cout << "SLAT maths.h" << std::endl;
}
//std::cout << "p_new in math: " << p_new << std::endl;
}
}
My problem is, that I am unable to access the value of p_org or p_new. At the beginning I would like to give out the value of p_org and p_new. If I try it with a *, the compiler is complaining: error: no operator "<<" matches these operands
operand types are: std::basic_ostream> << std::vector>
std::cout << "Control at the beginning: p_org: " << (*p_org) << " p_new: " << (*p_new) << " n_points: " << n_points << " s_begin: " << s_begin << " s_end: " << s_end << " total_length: " << total_length << std::endl;
If I leave the * off, I get the addresses of p_org and p_new.
At the end of the code I would like to write the new value to p_new. If I use * to access the value, the compiler is complaining, if I leave it off, its complaining too with the following message:
error: no operator "-" matches these operands
operand types are: std::vector<double, std::allocator<double>> - double
p_new[i]=p_org[0]-(total_length*sn[i]);
^
I tried to understand both problems, but until now I had no success.
Thanks for your advice.
Your issue with the compiler error can be cut down to a very simple program.
#include <vector>
void foo(std::vector<int>* pV)
{
pV[0] = 10; // error.
}
int main()
{
std::vector<int> v(10);
foo(&v);
}
The issue is that operator[] as done above works for objects and references, not pointers. Since pv is a pointer, you must dereference it first to obtain the object, and then apply [] to the dereferenced pointer.
void foo(std::vector<int>* pV)
{
(*pV)[0] = 10; // No error
}
The other form of calling operator[] can be also used, but is a bit more verbose:
void foo(std::vector<int>* pV)
{
pv->operator[](0) = 10; // No error
}
However, to alleviate having to do this, pass the vector by reference. Then the "normal" way of using operator[] can be used.
#include <vector>
void foo(std::vector<int>& pV)
{
pV[0] = 10; // No error.
}
int main()
{
std::vector<int> v(10);
foo(v);
}