question

GalqDimitrova-5295 avatar image
0 Votes"
GalqDimitrova-5295 asked RLWA32-6355 commented

Program Error Visual Studio

I have this error in visual studio, and i dont know what does mean
Link for picture for the error https://ibb.co/FswnRmj


[code]

include <ostream>

include <string>

include <istream>

class Stock {

 int code;
 std::string name;
 double valuePerUnit;
 int noUnits;

public:
Stock();

 Stock(int c, std::string n, double v, int nu);
        
 void setCode(int c);
        
 void setName(std::string n);
        
 void setValuePerUnit(double v);
        
 void setNoUnits(int n);
        
 int getCode();
        
 std::string getName();
        
 double getValuePerUnit();
        
 double getNoUnits();
        
 double calcAmount();

 friend std::ostream & operator<<(std::ostream &out, const Stock &s);
        
 friend std::istream &operator>>(std::istream &in, Stock& s);


};
[/code]


[/code]

include "Stock.h"

include <iostream>


Stock::Stock() {
code = 0;
name = "";
valuePerUnit = 1;
noUnits = 1;
}
Stock::Stock(int c, std::string n, double v, int nu)
{
code = c;
name = n;
valuePerUnit = v;
noUnits = nu;
}

void Stock::setCode(int c)
{
code = c;
}

void Stock::setName(std::string n)
{
name = n;
}

void Stock::setValuePerUnit(double v)
{
valuePerUnit = v;
}

void Stock::setNoUnits(int n)
{
noUnits = n;
}

int Stock::getCode()
{
return code;
}

std::string Stock::getName()
{
return name;
}

double Stock::getValuePerUnit()
{
return valuePerUnit;;
}

double Stock::getNoUnits()
{
return noUnits;;
}

double Stock::calcAmount()
{
return getNoUnits() * getValuePerUnit();
}

std::ostream& operator << (std::ostream &out, Stock &s)
{
out << "\nCode : " << s.getCode();
out << "\nName : " << s.getName();
out << "\nNumber Of Units : " << s.getNoUnits();
out << "\nCost of Unit : " << s.getValuePerUnit();
out << "\nTotal : " << s.calcAmount() << std::endl;

 return out;

}

std::istream& operator >> (std::istream& in, Stock& s)
{
std::cout<<"Enter Stock Code ";
in>>s.code;

 std::cout<<"Enter Stock Name ";
 in>>s.name;
    
 std::cout<<"Enter value per Unit : ";
 in>>s.valuePerUnit;

 std::cout<<"Enter Number of Units : ";
 in>>s.noUnits;
    
 return in;

}
[/code]

c++
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Your declaration for operator << in the class specifies the second argument as const. Your function definition does not. Try fixing that.

0 Votes 0 ·
MinxinYu-MSFT avatar image
0 Votes"
MinxinYu-MSFT answered MinxinYu-MSFT edited

Hi, @GalqDimitrova-5295
Error:

 friend std::ostream & operator<<(std::ostream &out, const Stock &s);

 std::ostream& operator << (std::ostream& out, Stock&  s)
 {
       out << "\nCode : " << s.getCode();
       out << "\nName : " << s.getName();
       out << "\nNumber Of Units : " << s.getNoUnits();
       out << "\nCost of Unit : " << s.getValuePerUnit();
       out << "\nTotal : " << s.calcAmount() << std::endl;
       return out;
 }

The declaration of the symbol isn't spelled the same as the definition of the symbol. Verify you use the correct spelling and capitalization in both the declaration and the definition, and wherever the symbol is used or called. You could refer to the document: Linker Tools Error LNK2019.



Best regards,

Minxin Yu


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.











5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 commented

I cut and pasted the questioner's code into an newly created empty VS2019 project (version16.11.5) and it compiled and linked without error.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Even with the missing hashtags?

0 Votes 0 ·

Of course not. The missing hash tags are a common problem we see when questioners post code to this site. I did take the liberty of adding them to the include statements. But that's all I did.

0 Votes 0 ·