friend function, can't figure error
Dec 3, 2011 at 1:15pm UTC
Hello, I'm following my lecture notes example and have an error popping out stating:
1 2 3
/tmp/cckhUI3q.o: In function `main':
friendtry2.cpp:(.text+0x9c): undefined reference to `Product::Product(int, double)'
collect2: ld returned 1 exit status
Any idea how can i solve this? Been trying different example from the nte but still having same error. Maybe i didn't declare certain things properly...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <iostream>
using namespace std;
class Product
{
friend void displayInfo(Product);
private :
int id;
double price;
public :
Product(int = 0, double = 0.0);
};
void displayInfo(Product item)
{
cout<<"Product id is #" <<item.id<<endl;
cout<<"roduct price is $" <<item.price<<endl;
}
int main()
{
Product oneItem(123, 12.95);
displayInfo(oneItem);
}
Dec 3, 2011 at 1:47pm UTC
I don't know I'm doing the right thing to demonstrate friend function usage, but I solve this by defining the constructors.. like this..
correct me if im wrong
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <iostream>
using namespace std;
class Product
{
friend void displayInfo(Product);
private :
int id;
double price;
public :
Product();
Product(int , double );
};
Product::Product()
{
id = 0;
price = 0.0;
}
Product::Product(int i, double p)
{
id = i;
price = p;
}
void displayInfo(Product item)
{
cout<<"Product id is #" <<item.id<<endl;
cout<<"Product price is $" <<item.price<<endl;
}
int main()
{
Product oneItem(123, 12.95);
displayInfo(oneItem);
}
Topic archived. No new replies allowed.