Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have seen other similar questions being asked but I could not figure out what the problem is. I have a declaration in an inventory class as:
class Inventory
{
Public:
void print();
void sell(Item*);
void add();
void find(string);
Private:
Item* first;
}
And then in the inventory.cpp I have:
void sell(Item* item_name)
{
..........................
}
And the error comes from calling it in main() as:
Inventory store_inventory;
Item* cur_item;
cout<<"Item name: ";
string name;
cin>>name;
cur_item = find(name); //find returns Item*
store_inventory.sell(cur_item);
The error is one the line for the call to sell. Any ideas?
The definitions need to indicate that it is a member function of Inventory:
void Inventory::sell( Item* item_name )
{
// ...
}
Also, there are issues with the find() function. You declare it as a void member function, but use at as if it is a free function that returns an Item*.
Is Item a class?
You should change the void type to the type you want to be returned item.
Also whem you call your sell function, at the parameter if you want to pass the same pointer, you must use the & before the variable name like sell(item* &varname).
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
Is there any way to use a class member function from outside?
As far as I know for this it would be necessary to somehow inject a private component into it.
For example (this is just an example the notFooClass function does not compile):
class FooClass{
private:
int i;
public:
FooClass(int x){
this->i = x;
}
int f(int num){
return i+num;
}
};
int notFooClass(int num1, int num2){
return FooClass(num1)::f(num2); //
}
int main(){
FooClass x(10);
std::cout<<x.f(5)<<std::endl;
std::cout<<notFooClass(10, 5)<<std::endl;
return 0;
The output should be:
15
15
Is it even possible to do something similar?
public methods can be called from outside. Thats what they are for.
notFooClass creates an instance and calls a member function. Thats basically the same as you do in main. The difference is only that you are using an unnamed temporary and wrong syntax:
int notFooClass(int num1, int num2){
return FooClass(num1).f(num2);
}
or with a named object to illustrate the similarity to main:
int notFooClass(int num1, int num2){
FooClass x(num1);
return x.f(num2);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
Question
You have to create a class, named Student, representing the student's details, as mentioned above, and store the data of a student. Create setter and getter functions for each element; that is, the class should at least have following functions:
get_age, set_age
get_first_name, set_first_name
get_last_name, set_last_name
get_standard, set_standard
Also, you have to create another method to_string() which returns the string consisting of the above elements, separated by a comma(,). You can refer to stringstream for this.
code:
#include <iostream>
using namespace std;
class Student{
private:
int age;
string fname;
string lname;
int std;
public:
void set_age(int a){age=a;;}
void set_fname(string f){fname=f;} //fname= first name
void set_lname(string l){lname=l;} //lname= last name
void set_std(int s){std=s;}
void get_age(){
cout<<age<<endl;
}
void get_fname(){
cout<<fname<<endl;
}
void get_lname(){
cout<<lname<<endl;
}
void get_std(){
cout<<std<<endl;
}
void to_string(){
string fl=lname+", "+fname; //fl stand for first and last
cout<<fl;
}
};
int main() {
Student z;
int a,s;
string f,l;
cin>>a;
z.set_age(a);
cin>>f;
z.set_fname(f);
cin>>l;
z.set_lname(l);
cin>>s;
z.set_std(s);
get_age();
to_string();
get_std();
return 0;
}
output
Solution.cpp:52:11: error: ‘get_age’ was not declared in this scope
cout<<get_age();
^~~~~~~
Solution.cpp:52:11: note: suggested alternative: ‘getdate’
cout<<get_age();
^~~~~~~
getdate
Solution.cpp:53:15: error: no matching function for call to ‘to_string()’
to_string();
^
You create one Student instance, z, and call misc. setter member functions on that instance. When you then call the functions to print some of the values you've set, you forgot to tell the compiler which instance you want to print the values for. Since you only have one instance, z, add that before the member functions:
z.get_age();
z.to_string();
z.get_std();
Improvement suggestion: Functions that does not return a value but that prints the value would be better named print-something rather than get-something.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The static function getROI always returns 1 and also causes a warning :
the address of 'static float Account::getROI()' will never be NULL
#include <iostream>
using namespace std;
class Account
{
private:
int account_no;
int balance;
static float ROI;
public:
void setBalance(int x){balance=x;}
int getBalance(){return balance;}
void setAccountNo(int y){account_no=y;}
int getAccountNo(){return account_no;}
static void setROI(float z){ROI=z;}
static float getROI(){return ROI;}
};
float Account::ROI =0;
int main()
{
cout << "STATIC" << endl;
Account obj;
obj.setAccountNo(3435647);
obj.setBalance(1000000);
obj.setROI(4.9);
cout<<"Account No : "<<obj.getAccountNo()<<endl;
cout<<"Balance = "<<obj.getBalance()<<endl;
cout<<"Rate of int= "<<obj.getROI;
return 0;
}
You want obj.getROI(), not obj.getROI
It says the address will never be zero.
Which is true ... if the address were zero, reading it would be de-referencing a null pointer.
So you think you're looking at the data, but you're really looking at a pointer to the data, and (I presume) comparing that address to zero.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include <iostream>
using namespace std;
void kghg();
void menu(float kgs);
float kghg(float kilogram, float kgs){
kilogram=menu(kgs);
float hektogram;
hektogram=(kilogram*10);
return hektogram;
}
int main()
{
menu();
kghg()
return 0;
}
void menu(float kgs){
cout<<"Input values : ";cin>>kgs;
}
void kghg(){
float hektogram, kilogram;
hektogram=kghg(kilogram);
cout<<"Result : "<<hektogram<<endl;
}
when I try to compile this code, then show notice " error : void value not ignored as it though to be" and "error : too few arguments 'void menu(float)'".
Please someone help me.
You call the function menu as
menu();
Though it is declared as
void menu(float kgs)
You said that the function would take a float argument, but did not pass it one.
As a side note, it looks like you are trying to cin a value into kgs. If you want this to act as you expect, you need to pass kgs by reference, otherwise you will input a value to a copy of kgs, then the original would be unchanged. You could change menu to
void menu(float& kgs)
Then call it as
float kg; // Declare a float variable
menu(kg); // Pass that float by reference to your function
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Constructor is here:
When I declare my setLeft() function, it tells me m_pLeft is not defined. I've tried moving it all over the place and can't get it to say it's anything other than undefined.
SetLeft is defined as
void setLeft(BookRecord *leftpointer){
*m_pLeft = *leftpointer;
}
#pragma once
class BookRecord
{
private:
char m_sName[100]; //unique names for each book
long m_lStockNum; //a stock number, similar to a barcode
int m_iClassification; //how a book should be classified, similar to a dewey decimal system
double m_dCost; //The price of the book
int m_iCount; //How many books are in stock
BookRecord *m_pLeft; //Left pointer for the tree
BookRecord *m_pRight; //right Pointer from the tree
public:
BookRecord(void);
BookRecord(char *name,long sn, int cl,double cost);
~BookRecord();
void getName(char *name);
void setName(char *Sname);
long getStockNum();
void setStockNum(long sn);
void getClassification(int& cl);
void setClassification(int cl);
double getCost();
void setCost(double c);
int getNumberInStock();
void setNumberInStock(int count);
void printRecord();
BookRecord getLeft();
BookRecord getRight();
void setLeft(BookRecord *leftpointer);
void setRight(BookRecord *rightpointer);
};
When I declare my setLeft() function, it tells me m_pLeft is not defined.
The error that you see is not coming from the declaration of setLeft() member function, but from its definition (the declaration is not referencing m_pLeft):
// This is incorrect - it will not compile
void setLeft(BookRecord *leftpointer) {
m_pLeft = leftpointer;
}
The problem with a definition like this is that the compiler treats it like a free-standing function, so m_pLeft member is not in scope. You need to tell the compiler that you are defining a member function, like this:
// This will compile
void BookRecord::setLeft(BookRecord *leftpointer) {
m_pLeft = leftpointer;
}