accessing a function using 2d array - c++

i want to access the seating[row][col].getFirstName but with the following code i am getting an error and the same error in strcmp in the if condition can anyone explain what does it mean
Error C3867 'Guest::getFirstName': non-standard syntax; use '&' to
create a pointer to member
Auditorium.cpp
Auditorium::Auditorium(int rowNum, int columnNum) {
rowNum1 = rowNum;
columnNum1 = columnNum;
Guest** seating =new Guest*[rowNum];
for (int i = 0; i < rowNum; i++)
{
seating[i] = new Guest[columnNum];
Guest();
}
}
bool Auditorium::assignGuestAt(int row,int col, Guest* tempGuest){
if ((strcmp(seating[row][col].getFirstName ,"???")==0)&& (strcmp(seating[row][col].getLastName , "???")==0)) \\ error
{
for (int i = 0; i < row; i++)
{
seating[row][col].getFirstName= tempGuest->getFirstName;\\ error
seating[row][col].getLastName = tempGuest->getLastName;\\ error
return 1;
}
}
else {
return 0;
}
}
Auditorium.h
#include<iostream>
#include "guest.h"
using namespace std;
class Auditorium
{
private:
Guest **seating;
public:
Auditorium(int, int);
Guest* getGuestAt(int, int);
bool assignGuestAt(int, int, Guest *);
bool checkBoundaries(int, int);
void toString();
};
guest.cpp
#include<iostream>
#include"guest.h"
using namespace std;
Guest::Guest()
{
strcpy_s(firstName,"???");
strcpy_s(lastName, "???");
}
char* Guest::getFirstName()
{
return firstName;
}
char* Guest::getLastName()
{
return lastName;
}

Related

How to fix: Exception thrown: read access violation. **_Pnext** was 0xFDFDFE05

I'm trying to "build" a house which contains rooms. My teacher told me to do it with composition. My idea was to generate as many Rooms objects as many rooms my house has. I tried it with the following code but in the house.cpp's constructor my for cycle runs only once than it crashes at the line rooms[i] = load; with Exception thrown: read access violation.
_Pnext was 0xFDFDFE05.
Here's my code:
main.cpp:
'''
#include <iostream>
#include "House.h"
#include "Rooms.h"
using namespace std;
int main()
{
Rooms basic("basic");
House house(basic,2);
house.setRooms();
house.setWhichroom(1);
cout << house.getRooms().getRoomname();
return 0;
}
'''
house.h:
'''
#pragma once
#include <iostream>
#include <string>
#include "Rooms.h"
using namespace std;
class House
{
Rooms* rooms;
unsigned roomcount;
unsigned whichroom = 0;
public:
House();
House(const Rooms&, unsigned);
House(unsigned);
~House();
void setRoomcount(unsigned);
unsigned getRoomcount()const;
void setWhichroom(unsigned );
unsigned getWhichroom()const;
void setRooms();
Rooms getRooms()const;
};
'''
house.cpp:
'''
#include "House.h"
House::House(const Rooms& load, unsigned rc)
{
roomcount = rc;
for (int i = 0; i < roomcount; i++)
{
rooms = new Rooms;
rooms[i] = load;
}
}
House::House(unsigned x):roomcount(x)
{
}
House::~House()
{
roomcount = 0;
delete rooms;
}
void House::setRoomcount(unsigned x)
{
roomcount = x;
}
unsigned House::getRoomcount() const
{
return roomcount;
}
void House::setWhichroom(unsigned x)
{
whichroom = x;
}
unsigned House::getWhichroom() const
{
return whichroom;
}
void House::setRooms()
{
for (unsigned i = 1; i <= roomcount; i++)
{
char c = i;
string s = "room";
s += c;
rooms[i].setRoomname(s);
}
}
Rooms House::getRooms() const
{
return rooms[whichroom];
}
'''
rooms.h:
'''
#pragma once
#include <iostream>
#include "Things.h"
using namespace std;
class Rooms
{
protected:
string roomname;
Things things;
public:
Rooms();
Rooms(string);
~Rooms();
void setRoomname(string);
string getRoomname()const;
void setThings();
unsigned getThings()const;
};
'''
rooms.cpp:
'''
#include "Rooms.h"
Rooms::Rooms()
{
}
Rooms::Rooms(string name)
{
roomname = name;
}
Rooms::~Rooms()
{
}
void Rooms::setRoomname(string name)
{
roomname = name;
}
string Rooms::getRoomname() const
{
return roomname;
}
void Rooms::setThings()
{
things.setOnoff();
}
unsigned Rooms::getThings() const
{
return things.getOnoff();
}
'''
The problem is in the House constructor, where you have
for (int i = 0; i < roomcount; i++)
{
rooms = new Rooms;
rooms[i] = load;
}
You will create one single Rooms object, but treat it as an array of multiple rooms.
You also create and create and create new Room objects each iteration of the loop, making you loose the earlier objects.
The simple solution, if you need to continue to use explicit memory handling yourself, is to allocate the objects once and before the loop:
rooms = new Rooms[roomcount];
for (int i = 0; i < roomcount; i++)
{
rooms[i] = load;
}
Since you now use new[] you must match it with a delete[] in the destructor. And you need to learn about the rules of three and five.
To simplify your code I implore you to use a std::vector instead, as that will make life so much simpler for you as a C++ programmer.

Reference to a non-const object cannot be initialized with an r-value of that object

I create an object and try to pass that object through multiple function by reference
#include<iostream>
#include <string>
#include "DBMS.h"
using namespace std;
void Home(DBMS &);
int main()
{
DBMS dbms();
Home(dbms); // this is where the error is
return 0;
}
void Home(DBMS &dbms)
{
string dbName;
dbms.addDatabase(dbName);
}
and this is DBMS.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "Database.h"
using namespace std;
class DBMS
{
public:
DBMS();
void addDatabase(string);
Database& getDatabase(string);
Database& getDatabaseByIndex(int);
int getDatabaseIndex(string);
void removeDatabase(string);
int size();
~DBMS();
private:
vector <Database> dbList;
string error;
};
DBMS::DBMS()
{
}
void DBMS::addDatabase(string tbNames)
{
vector<string> TB_Names = tokenize(tbNames);
int size = TB_Names.size();
for (int i = 0; i < size; i++)
{
if (getDatabaseIndex(TB_Names[i]) == -1)
{
Database tb(TB_Names[i]);
dbList.push_back(tb);
}
else
throw "Already esited Database with given name";
}
}
Database& DBMS::getDatabase(string tbName)
{
int i;
int size = dbList.size();
for (i = 0; i < size; i++)
{
if (dbList[i].getName() == tbName)
return dbList[i];
}
throw invalid_argument("Database not found");
}
Database& DBMS::getDatabaseByIndex(int index)
{
return dbList[index];
}
void DBMS::removeDatabase(string TB_Name)
{
int i;
int size = dbList.size();
for (i = 0; i < size; i++)
{
if (dbList[i].getName() == TB_Name)
{
dbList.erase(dbList.begin() + i);
break;
}
}
if (i == size)
throw invalid_argument("Field not found");
}
int DBMS::getDatabaseIndex(string TB_Name)
{
int size = dbList.size();
for (int i = 0; i < size; i++)
{
if (dbList[i].getName() == TB_Name)
return i;
}
return -1;
}
int DBMS::size()
{
return dbList.size();
}
DBMS::~DBMS()
{
}
(Database type is just another class i create. Nothing special about it. Don't worry about it (Unless you think I have to))
The error statement is : >a reference of type "DBMS &" (a non-const-qualified) cannot be initialized with an value of "DBMS()"
I found a suggestion that i should fix void Home(DBMS &dbms) to void Home(const DBMS &dbms)
but if i do that, i can't use addDatabase() function
How can i fix this?
This declaration
DBMS dbms();
is a vexing parse. You are not declaring a variable named dbms, but instead you are actually declaring a function named dbms that takes no arguments, and returns a DBMS. Passing this object to a function expecting a DBMS object will not work.
You can fix this with:
DBMS dbms{};

Keep getting "cygwin_exception::open_stackdumpfile" while dynamically allocating 2-D array in a class

I have been attempting to write this program where I am required to utilize dynamically allocated arrays to print out a 2d matrix. I am only to write the cpp files and not allowed to modify anything in the header files.
I keep getting an exception
0 [main] review2_cis17c_objectarray 4018 cygwin_exception::open_stackdumpfile: Dumping stack trace to review2_cis17c_objectarray.exe.stackdump
I am relatively new to learning c++; after contemplating, I think something is wrong in my PlusTab.cpp, where I am trying to assign an allocated address to a constructor-defined array in a class. Can someone please help and let me know here I did wrong in the project? Thank you very much!
AbsRow.h:
class AbsRow {
protected:
int size;
int *rowData;
public:
virtual int getSize()const = 0;
virtual int getData(int)const = 0;
};
AbsTabl.h:
class AbsTabl {
protected:
int szRow;
int szCol;
RowAray **columns;
public:
virtual int getSzRow()const = 0;
virtual int getSzCol()const = 0;
virtual int getData(int,int)const = 0; };
PlusTab.h
class PlusTab:public Table {
public:
PlusTab(unsigned int r,unsigned int c):Table(r,c){};
PlusTab operator+(const PlusTab &);
};
RowAray.h
class RowAray:public AbsRow {
public:
RowAray(unsigned int);
virtual ~RowAray();
int getSize()const{return size;}
int getData(int i)const{
if(i>=0&&i<size)return rowData[i];
else return 0;}
void setData(int,int);
};
Table.h
#include "AbsTabl.h"
class Table:public AbsTabl {
public:
Table(unsigned int,unsigned int);
Table(const Table &);
virtual ~Table();
int getSzRow()const {return szRow;}
int getSzCol()const {return szCol;}
int getData(int,int)const;
void setData(int,int,int);
};
PlusTab.cpp:
#include "PlusTab.h"
PlusTab PlusTab::operator+(const PlusTab &t) {
PlusTab tab(this->getSzRow(), this->getSzCol());
for(int i = 0; i < tab.getSzRow(); i++) {
for (int j = 0; j <tab.getSzCol(); j++) {
(tab.columns[i])->setData(j, this->getData(i,j) + t.getData(i,j));
}
}
return tab;
}
RowAray.cpp:
#include "RowAray.h"
RowAray::RowAray(unsigned int c) {
size = c;
rowData = new int[c];
}
RowAray::~RowAray() {
delete []rowData;
}
void RowAray::setData(int i, int value) {
rowData[i] = value;
}
Table.cpp:
#include "Table.h"
#include <cstdlib>
Table::Table(unsigned int r, unsigned int c) {
szRow = r;
szCol = c;
columns = new RowAray*[r];
for (int i = 0; i < r; i++) {
columns[i] = new RowAray(c);
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
columns[i]->setData(j, (rand()%90 + 10));
}
}
}
Table::~Table() {
for (int i = 0; i < szRow; i++) {
delete []columns[i];
}
delete []columns;
}
Table::Table(const Table &t) {
szRow = t.szRow;
szCol = t.szCol;
columns = t.columns;
};
int Table::getData(int r ,int c) const {
return columns[r]->getData(c);
};
void Table::setData(int r, int c, int value) {
columns[r]->setData(c,value);
}
and finally my main.cpp, which I am not allowed to modify either.
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;
//User Libraries
#include "PlusTab.h"
//Global Constants
//Function Prototype
void prntTab(const Table &);
//Execution Begins Here!
int main(int argc, char** argv) {
//Initialize the random seed
srand(static_cast<unsigned int>(time(0)));
//Declare Variables
int rows=3,cols=4;
//Test out the Tables
PlusTab tab1(rows,cols);
PlusTab tab2(tab1);
PlusTab tab3=tab1+tab2;
// Print the tables
cout<<"Abstracted and Polymorphic Print Table 1 size is [row,col] = ["
<<rows<<","<<cols<<"]";
prntTab(tab1);
cout<<"Copy Constructed Table 2 size is [row,col] = ["
<<rows<<","<<cols<<"]";
prntTab(tab2);
cout<<"Operator Overloaded Table 3 size is [row,col] = ["
<<rows<<","<<cols<<"]";
prntTab(tab3);
//Exit Stage Right
return 0;
}
void prntTab(const Table &a){
cout<<endl;
for(int row=0;row<a.getSzRow();row++){
for(int col=0;col<a.getSzCol();col++){
cout<<setw(4)<<a.getData(row,col);
}
cout<<endl;
}
cout<<endl;
}
I apologize for this massive amount of code. This is my first time posting, will learn to use the website! I appreciate your help:)

no matching function for call [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
i am new to c++ and i am currently trying to write a program that uses main to call functions which are seperately written and i tried to write the definitions for the header file declarations and i am getting errors with some functions which i have marked in the code
Store.hpp
#ifndef STORE_HPP
#define STORE_HPP
class Product;
class Customer;
#include<string>
#include "Customer.hpp"
#include "Product.hpp"
class Store
{
private:
std::vector<Product*> inventory;
std::vector<Customer*> members;
public:
void addProduct(Product* p);
void addMember(Customer* c);
Product* getProductFromID(std::string);
Customer* getMemberFromID(std::string);
void productSearch(std::string str);
void addProductToMemberCart(std::string pID, std::string mID);
void checkOutMember(std::string mID);
};
#endif
i am having trouble writing the code for that function help me
store.cpp
#include <iostream>
#include <string.h>
#include "Customer.hpp"
#include "Store.hpp"
#include "Product.hpp"
using namespace std;
string id;
void Store::addProduct(Product* p) //error 1 no matching function
{
Product* p(std::string id, std::string t, std::string d, double p, int qa);
inventory.push_back(p);
}
void Store:: addMember(Customer* c)
{
members.push_back(c->getAccountID());
}
Product* Store::getProductFromID(std::string id)
{
for(int i = 0; i < inventory.size(); i++)
{
Product* p=inventory.at(i);
if(p->getIdCode()= id)
{
return p;
}
}
return NULL;
}
Customer* Store:: getMemberFromID(std::string id)
{
for(int i = 0; i < members.size(); i++)
{
Customer* c = members.at(i);
if(c->getAccountID() == id)
{
return c;
}
}
return NULL;
}
void std::Store productSearch(std::string str)
{
for(int i = 0; i < inventory.size(); i++)
{
if(inventory[i] == str)
{
Product stud(inventory[i],inventory[i+1],inventory[i+2],inventory[i+3],inventory[i+4]);
cout<<getIdCode();
cout<<getTitle();
cout<<getDescription();
cout<<getPrice();
cout<<getQuantityAvailable();
}
}
}
void addProductToMemberCart(std::string pID, std::string mID)
{
cout<<"adding to cart"<<endl;
getMemberFromID(mID)->addProductToCart(pID);
}
void checkOutMember(std::string mID)
{
Customer* c=getAccountID(mID)
mID=getMemberFromID(std::string mID);
if(mID=="NULL")
{
cout<<mID<<"is not found"<<endl;
}
}
customer.hpp
#ifndef CUSTOMER_HPP
#define CUSTOMER_HPP
#include<vector>
#include "Product.hpp"
class Customer
{
private:
std::vector<std::string> cart;
std::string name;
std::string accountID;
bool premiumMember;
public:
Customer(std::string n, std::string a, bool pm);
std::string getAccountID();
//std::vector getCart();
void addProductToCart(std::string);
bool isPremiumMember();
void emptyCart();
};
#endif
product.hpp
#ifndef PRODUCT_HPP
#define PRODUCT_HPP
#include<vector>
class Product
{
private:
std::string idCode;
std::string title;
std::string description;
double price;
int quantityAvailable;
public:
Product(std::string id, std::string t, std::string d, double p, int qa);
std::string getIdCode();
std::string getTitle();
std::string getDescription();
double getPrice();
int getQuantityAvailable();
void decreaseQuantity();
};
#endif
You code issues lots of warnings and errors as stands.
If you find yourself in this situation and can't figure out what one of them means, try to fix some of the others.
Your main problem is in addProduct, but there are others
using namespace std;
string id; //<---- what's this for?
void Store::addProduct(Product* p) //error 1 no matching function
{
//Product* p(std::string id, std::string t, std::string d, double p, int qa);
//<--- This line had the error and isn't needed
inventory.push_back(p);
}
void Store::addMember(Customer* c)
{
// members.push_back(c->getAccountID()); //<--- this errors too
members.push_back(c);
}
Product* Store::getProductFromID(std::string id)
{
for (size_t i = 0; i < inventory.size(); i++)
{
Product* p = inventory.at(i);
//if (p->getIdCode() = id) //<-- be careful with = and ==
if (p->getIdCode() == id) //<---
{
return p;
}
}
return NULL;
}
Customer* Store::getMemberFromID(std::string id)
{
for (size_t i = 0; i < members.size(); i++)
{
Customer* c = members.at(i);
if (c->getAccountID() == id)
{
return c;
}
}
return NULL;
}
//void std::Store productSearch(std::string str)
void Store::productSearch(std::string str) // <---- note this change too
{
for (size_t i = 0; i < inventory.size(); i++)
{
//if (inventory[i] == str) //<<--------!
if (inventory[i]->getDescription() == str)
{
//Product stud(inventory[i], inventory[i + 1], inventory[i + 2], inventory[i + 3], inventory[i + 4]);
// This is five Products from the inventory, not the i-th product in your invetory
Product stud(*inventory[i]);//<---- I assume
cout << stud.getIdCode();
cout << stud.getTitle();
cout << stud.getDescription();
cout << stud.getPrice();
cout << stud.getQuantityAvailable();
}
}
}
void Store::addProductToMemberCart(std::string pID, std::string mID)//<--- note note std::Store addProductToMemberCart
{
cout << "adding to cart" << endl;
getMemberFromID(mID)->addProductToCart(pID);
}
void Store::checkOutMember(std::string mID)//<---
{
//Customer* c = getAccountID(mID);//<<---?
//mID = getMemberFromID(std::string mID); //<---?
Customer* c = getMemberFromID(mID); //Just this?
if (c == NULL)//<---rather than "NULL" but nullptr might be better
{ // or not using pointers at all
cout << mID << "is not found" << endl;
}
}

Pass Vector by reference and Get Error

Following code is for test the pass the reference of a vector to a function. However, it will have some unknown fault. I got the error message from gdb as following:
Source is:
#include<iostream>
#include<vector>
using namespace std;
class ROLE
{
public:
ROLE(int);
int HP;
};
ROLE::ROLE (int input)
{
HP = input;
}
void checkVector(vector<ROLE>&input);
int main()
{
vector<ROLE> R;
ROLE K(102);
R.push_back(K);
K.HP = 111;
R.push_back(K);
checkVector(R);
return 1;
}
void checkVector(vector <ROLE> & input)
{
cout<<"size of vector "<<input.size()<<endl;
for(int i =0; i<input.size();i++)
{
cout<< input[i].HP<<endl;
}
}
I can't find the reason of the error. Any idea is appreciated!
I realized that error probably from the push_back, because I also get the error if I modified the code as following. Are there any idea about what happened?
#include<iostream>
#include<vector>
using namespace std;
class ROLE
{
public:
ROLE(int);
int HP;
};
ROLE::ROLE (int input)
{
HP = input;
}
int main()
{
vector<ROLE> R;
ROLE K(0);
K.HP=1;
R.push_back(K);
K.HP = 113;
R.push_back(K);
K.HP = 111;
R.push_back(K);
return 0;
}
Possible answer:
I get one way which can avoid the error.
Following is the no-error source.
#include<iostream>
#include<vector>
using namespace std;
class ROLE
{
public:
ROLE(int);
void constructAry(int inputHP);
int HP;
};
ROLE::ROLE (int input)
{
HP = input;
}
void ROLE::constructAry(int inputHP)
{
HP = inputHP;
}
void checkVector(vector<ROLE>&input);
int main()
{
vector<int> test;
vector<ROLE> R;
ROLE K(0);
K.constructAry(1);
R.push_back(K);
K.constructAry(2);
R.push_back(K);
K.constructAry(3);
R.push_back(K);
K.constructAry(4);
R.push_back(K);
checkVector(R);
return 1;
}
void checkVector(vector <ROLE> & input)
{
cout<<"size of vector "<<input.size()<<endl;
for(int i =0; i<input.size();i++)
{
cout<< input[i].HP<<endl;
}
}
So it seems that using a constructor-like function to modify the value of the same object and push_back the object in the vector can effectively avoid the bug.
The reason is ambiguous for me. Maybe the memory address's matter.