passing reference to object c++
Okay so I'm doing a little for-fun project, I'm an experienced LAMP developer and I decided to mess around with C++
This code is just my attempt at making a working application thats a little bit more difficult than "Hello World" My problem here is in void Defer(). I want to pass a reference to class PoS to my ItemList, so that it can leverage the tax values when returning receipts and calculations
but here is my error that I'm getting:
"ItemList::ItemList(PoS &pos)" provides no initializer for: -- reference member "ItemList::client"
class PoS {
private:
/*
States
0: Main Menu
1: Order Menu
2: Edit Menu
3: Tax Menu
4: Business Menu
*/
int State;
vector<ItemList> Orders;
bool IsLiveFlag;
double Tax = 0.06;
public:
PoS() {
// Constructor
}
// .......
void Defer() {
string Command;
gotoMain:
ShowMain();
cin>>Command;
if (tolowercase(Command) == "1") {
ItemList order(*this);
Orders.push_back(order);
State = 1;
Orders[Orders.size() - 1].Defer();
State = 0;
}
}
}
class ItemList {
private:
vector<Item> Items;
double Subtotal;
double Tendered;
bool Paid;
PoS& client;
public:
ItemList(PoS& pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
PoS& client = pos;
}
For some reason I can't get a reference to PoS to send over to my ItemList as a parameter, its really frustrating!
c++
add a comment |
Okay so I'm doing a little for-fun project, I'm an experienced LAMP developer and I decided to mess around with C++
This code is just my attempt at making a working application thats a little bit more difficult than "Hello World" My problem here is in void Defer(). I want to pass a reference to class PoS to my ItemList, so that it can leverage the tax values when returning receipts and calculations
but here is my error that I'm getting:
"ItemList::ItemList(PoS &pos)" provides no initializer for: -- reference member "ItemList::client"
class PoS {
private:
/*
States
0: Main Menu
1: Order Menu
2: Edit Menu
3: Tax Menu
4: Business Menu
*/
int State;
vector<ItemList> Orders;
bool IsLiveFlag;
double Tax = 0.06;
public:
PoS() {
// Constructor
}
// .......
void Defer() {
string Command;
gotoMain:
ShowMain();
cin>>Command;
if (tolowercase(Command) == "1") {
ItemList order(*this);
Orders.push_back(order);
State = 1;
Orders[Orders.size() - 1].Defer();
State = 0;
}
}
}
class ItemList {
private:
vector<Item> Items;
double Subtotal;
double Tendered;
bool Paid;
PoS& client;
public:
ItemList(PoS& pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
PoS& client = pos;
}
For some reason I can't get a reference to PoS to send over to my ItemList as a parameter, its really frustrating!
c++
1
Seek ye the mighty Member Initializer list! In C++ all objects must be fully constructed before entering the body of a constructor. Since a reference cannot be reassigned after initialization, you can'tclient = pos;.PoS& client = pos;won't do what you need either. That declared a local variable.
– user4581301
Dec 29 '18 at 5:48
The definition ofItemListneeds to be before the usage inDefer, not after.
– Peter
Dec 29 '18 at 5:48
add a comment |
Okay so I'm doing a little for-fun project, I'm an experienced LAMP developer and I decided to mess around with C++
This code is just my attempt at making a working application thats a little bit more difficult than "Hello World" My problem here is in void Defer(). I want to pass a reference to class PoS to my ItemList, so that it can leverage the tax values when returning receipts and calculations
but here is my error that I'm getting:
"ItemList::ItemList(PoS &pos)" provides no initializer for: -- reference member "ItemList::client"
class PoS {
private:
/*
States
0: Main Menu
1: Order Menu
2: Edit Menu
3: Tax Menu
4: Business Menu
*/
int State;
vector<ItemList> Orders;
bool IsLiveFlag;
double Tax = 0.06;
public:
PoS() {
// Constructor
}
// .......
void Defer() {
string Command;
gotoMain:
ShowMain();
cin>>Command;
if (tolowercase(Command) == "1") {
ItemList order(*this);
Orders.push_back(order);
State = 1;
Orders[Orders.size() - 1].Defer();
State = 0;
}
}
}
class ItemList {
private:
vector<Item> Items;
double Subtotal;
double Tendered;
bool Paid;
PoS& client;
public:
ItemList(PoS& pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
PoS& client = pos;
}
For some reason I can't get a reference to PoS to send over to my ItemList as a parameter, its really frustrating!
c++
Okay so I'm doing a little for-fun project, I'm an experienced LAMP developer and I decided to mess around with C++
This code is just my attempt at making a working application thats a little bit more difficult than "Hello World" My problem here is in void Defer(). I want to pass a reference to class PoS to my ItemList, so that it can leverage the tax values when returning receipts and calculations
but here is my error that I'm getting:
"ItemList::ItemList(PoS &pos)" provides no initializer for: -- reference member "ItemList::client"
class PoS {
private:
/*
States
0: Main Menu
1: Order Menu
2: Edit Menu
3: Tax Menu
4: Business Menu
*/
int State;
vector<ItemList> Orders;
bool IsLiveFlag;
double Tax = 0.06;
public:
PoS() {
// Constructor
}
// .......
void Defer() {
string Command;
gotoMain:
ShowMain();
cin>>Command;
if (tolowercase(Command) == "1") {
ItemList order(*this);
Orders.push_back(order);
State = 1;
Orders[Orders.size() - 1].Defer();
State = 0;
}
}
}
class ItemList {
private:
vector<Item> Items;
double Subtotal;
double Tendered;
bool Paid;
PoS& client;
public:
ItemList(PoS& pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
PoS& client = pos;
}
For some reason I can't get a reference to PoS to send over to my ItemList as a parameter, its really frustrating!
c++
c++
asked Dec 29 '18 at 5:43
user2471817user2471817
63
63
1
Seek ye the mighty Member Initializer list! In C++ all objects must be fully constructed before entering the body of a constructor. Since a reference cannot be reassigned after initialization, you can'tclient = pos;.PoS& client = pos;won't do what you need either. That declared a local variable.
– user4581301
Dec 29 '18 at 5:48
The definition ofItemListneeds to be before the usage inDefer, not after.
– Peter
Dec 29 '18 at 5:48
add a comment |
1
Seek ye the mighty Member Initializer list! In C++ all objects must be fully constructed before entering the body of a constructor. Since a reference cannot be reassigned after initialization, you can'tclient = pos;.PoS& client = pos;won't do what you need either. That declared a local variable.
– user4581301
Dec 29 '18 at 5:48
The definition ofItemListneeds to be before the usage inDefer, not after.
– Peter
Dec 29 '18 at 5:48
1
1
Seek ye the mighty Member Initializer list! In C++ all objects must be fully constructed before entering the body of a constructor. Since a reference cannot be reassigned after initialization, you can't
client = pos;. PoS& client = pos; won't do what you need either. That declared a local variable.– user4581301
Dec 29 '18 at 5:48
Seek ye the mighty Member Initializer list! In C++ all objects must be fully constructed before entering the body of a constructor. Since a reference cannot be reassigned after initialization, you can't
client = pos;. PoS& client = pos; won't do what you need either. That declared a local variable.– user4581301
Dec 29 '18 at 5:48
The definition of
ItemList needs to be before the usage in Defer, not after.– Peter
Dec 29 '18 at 5:48
The definition of
ItemList needs to be before the usage in Defer, not after.– Peter
Dec 29 '18 at 5:48
add a comment |
1 Answer
1
active
oldest
votes
A reference member variable must be initialized before the body of the constructor. That is the meaning of the compiler error message in your post.
Instead of
ItemList(PoS& pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
PoS& client = pos;
}
use
ItemList(PoS& pos) : client(pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
}
You can improve on it by using the same syntactic form to initialize the other member variables also.
ItemList(PoS& pos) : Subtotal(0),
Tendered(0),
Paid(0),
client(pos) {}
Can I ask you WHY I'm using : client(pos), both client and pos are the references I want... of PoS class... I just don't understand it, I don't wanna use the code above and not understand it! I appreciate the answer though but is there any way you can ellaborate a lil more? Whats the syntax for the lines above? Also, do I have to still define client in the class' scope as a private variable, if I'd like it to be private, is : client(pos) DECLARING the variable? So confused, I'm so sorry guys I came on here with so many questions!
– user2471817
Dec 29 '18 at 6:48
@user2471817, what do you mean by "I want... of PoS class ...?
– R Sahu
Dec 29 '18 at 6:54
Sorry, bad english there on my part! I'll correct it! Both "client" and "pos" are where I want the references to be contained of PoS
– user2471817
Dec 29 '18 at 9:05
now I'm getting PS C:cppAcePoS> g++ main.cpp In file included from PoS.h:1:0, from main.cpp:1: ItemList.h:10:9: error: 'PoS' does not name a type PoS& client; ^~~
– user2471817
Dec 29 '18 at 9:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967018%2fpassing-reference-to-object-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A reference member variable must be initialized before the body of the constructor. That is the meaning of the compiler error message in your post.
Instead of
ItemList(PoS& pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
PoS& client = pos;
}
use
ItemList(PoS& pos) : client(pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
}
You can improve on it by using the same syntactic form to initialize the other member variables also.
ItemList(PoS& pos) : Subtotal(0),
Tendered(0),
Paid(0),
client(pos) {}
Can I ask you WHY I'm using : client(pos), both client and pos are the references I want... of PoS class... I just don't understand it, I don't wanna use the code above and not understand it! I appreciate the answer though but is there any way you can ellaborate a lil more? Whats the syntax for the lines above? Also, do I have to still define client in the class' scope as a private variable, if I'd like it to be private, is : client(pos) DECLARING the variable? So confused, I'm so sorry guys I came on here with so many questions!
– user2471817
Dec 29 '18 at 6:48
@user2471817, what do you mean by "I want... of PoS class ...?
– R Sahu
Dec 29 '18 at 6:54
Sorry, bad english there on my part! I'll correct it! Both "client" and "pos" are where I want the references to be contained of PoS
– user2471817
Dec 29 '18 at 9:05
now I'm getting PS C:cppAcePoS> g++ main.cpp In file included from PoS.h:1:0, from main.cpp:1: ItemList.h:10:9: error: 'PoS' does not name a type PoS& client; ^~~
– user2471817
Dec 29 '18 at 9:06
add a comment |
A reference member variable must be initialized before the body of the constructor. That is the meaning of the compiler error message in your post.
Instead of
ItemList(PoS& pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
PoS& client = pos;
}
use
ItemList(PoS& pos) : client(pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
}
You can improve on it by using the same syntactic form to initialize the other member variables also.
ItemList(PoS& pos) : Subtotal(0),
Tendered(0),
Paid(0),
client(pos) {}
Can I ask you WHY I'm using : client(pos), both client and pos are the references I want... of PoS class... I just don't understand it, I don't wanna use the code above and not understand it! I appreciate the answer though but is there any way you can ellaborate a lil more? Whats the syntax for the lines above? Also, do I have to still define client in the class' scope as a private variable, if I'd like it to be private, is : client(pos) DECLARING the variable? So confused, I'm so sorry guys I came on here with so many questions!
– user2471817
Dec 29 '18 at 6:48
@user2471817, what do you mean by "I want... of PoS class ...?
– R Sahu
Dec 29 '18 at 6:54
Sorry, bad english there on my part! I'll correct it! Both "client" and "pos" are where I want the references to be contained of PoS
– user2471817
Dec 29 '18 at 9:05
now I'm getting PS C:cppAcePoS> g++ main.cpp In file included from PoS.h:1:0, from main.cpp:1: ItemList.h:10:9: error: 'PoS' does not name a type PoS& client; ^~~
– user2471817
Dec 29 '18 at 9:06
add a comment |
A reference member variable must be initialized before the body of the constructor. That is the meaning of the compiler error message in your post.
Instead of
ItemList(PoS& pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
PoS& client = pos;
}
use
ItemList(PoS& pos) : client(pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
}
You can improve on it by using the same syntactic form to initialize the other member variables also.
ItemList(PoS& pos) : Subtotal(0),
Tendered(0),
Paid(0),
client(pos) {}
A reference member variable must be initialized before the body of the constructor. That is the meaning of the compiler error message in your post.
Instead of
ItemList(PoS& pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
PoS& client = pos;
}
use
ItemList(PoS& pos) : client(pos) {
Subtotal = 0;
Tendered = 0;
Paid = 0;
}
You can improve on it by using the same syntactic form to initialize the other member variables also.
ItemList(PoS& pos) : Subtotal(0),
Tendered(0),
Paid(0),
client(pos) {}
answered Dec 29 '18 at 5:48
R SahuR Sahu
165k1291184
165k1291184
Can I ask you WHY I'm using : client(pos), both client and pos are the references I want... of PoS class... I just don't understand it, I don't wanna use the code above and not understand it! I appreciate the answer though but is there any way you can ellaborate a lil more? Whats the syntax for the lines above? Also, do I have to still define client in the class' scope as a private variable, if I'd like it to be private, is : client(pos) DECLARING the variable? So confused, I'm so sorry guys I came on here with so many questions!
– user2471817
Dec 29 '18 at 6:48
@user2471817, what do you mean by "I want... of PoS class ...?
– R Sahu
Dec 29 '18 at 6:54
Sorry, bad english there on my part! I'll correct it! Both "client" and "pos" are where I want the references to be contained of PoS
– user2471817
Dec 29 '18 at 9:05
now I'm getting PS C:cppAcePoS> g++ main.cpp In file included from PoS.h:1:0, from main.cpp:1: ItemList.h:10:9: error: 'PoS' does not name a type PoS& client; ^~~
– user2471817
Dec 29 '18 at 9:06
add a comment |
Can I ask you WHY I'm using : client(pos), both client and pos are the references I want... of PoS class... I just don't understand it, I don't wanna use the code above and not understand it! I appreciate the answer though but is there any way you can ellaborate a lil more? Whats the syntax for the lines above? Also, do I have to still define client in the class' scope as a private variable, if I'd like it to be private, is : client(pos) DECLARING the variable? So confused, I'm so sorry guys I came on here with so many questions!
– user2471817
Dec 29 '18 at 6:48
@user2471817, what do you mean by "I want... of PoS class ...?
– R Sahu
Dec 29 '18 at 6:54
Sorry, bad english there on my part! I'll correct it! Both "client" and "pos" are where I want the references to be contained of PoS
– user2471817
Dec 29 '18 at 9:05
now I'm getting PS C:cppAcePoS> g++ main.cpp In file included from PoS.h:1:0, from main.cpp:1: ItemList.h:10:9: error: 'PoS' does not name a type PoS& client; ^~~
– user2471817
Dec 29 '18 at 9:06
Can I ask you WHY I'm using : client(pos), both client and pos are the references I want... of PoS class... I just don't understand it, I don't wanna use the code above and not understand it! I appreciate the answer though but is there any way you can ellaborate a lil more? Whats the syntax for the lines above? Also, do I have to still define client in the class' scope as a private variable, if I'd like it to be private, is : client(pos) DECLARING the variable? So confused, I'm so sorry guys I came on here with so many questions!
– user2471817
Dec 29 '18 at 6:48
Can I ask you WHY I'm using : client(pos), both client and pos are the references I want... of PoS class... I just don't understand it, I don't wanna use the code above and not understand it! I appreciate the answer though but is there any way you can ellaborate a lil more? Whats the syntax for the lines above? Also, do I have to still define client in the class' scope as a private variable, if I'd like it to be private, is : client(pos) DECLARING the variable? So confused, I'm so sorry guys I came on here with so many questions!
– user2471817
Dec 29 '18 at 6:48
@user2471817, what do you mean by "I want
... of PoS class ...?– R Sahu
Dec 29 '18 at 6:54
@user2471817, what do you mean by "I want
... of PoS class ...?– R Sahu
Dec 29 '18 at 6:54
Sorry, bad english there on my part! I'll correct it! Both "client" and "pos" are where I want the references to be contained of PoS
– user2471817
Dec 29 '18 at 9:05
Sorry, bad english there on my part! I'll correct it! Both "client" and "pos" are where I want the references to be contained of PoS
– user2471817
Dec 29 '18 at 9:05
now I'm getting PS C:cppAcePoS> g++ main.cpp In file included from PoS.h:1:0, from main.cpp:1: ItemList.h:10:9: error: 'PoS' does not name a type PoS& client; ^~~
– user2471817
Dec 29 '18 at 9:06
now I'm getting PS C:cppAcePoS> g++ main.cpp In file included from PoS.h:1:0, from main.cpp:1: ItemList.h:10:9: error: 'PoS' does not name a type PoS& client; ^~~
– user2471817
Dec 29 '18 at 9:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967018%2fpassing-reference-to-object-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Seek ye the mighty Member Initializer list! In C++ all objects must be fully constructed before entering the body of a constructor. Since a reference cannot be reassigned after initialization, you can't
client = pos;.PoS& client = pos;won't do what you need either. That declared a local variable.– user4581301
Dec 29 '18 at 5:48
The definition of
ItemListneeds to be before the usage inDefer, not after.– Peter
Dec 29 '18 at 5:48