please tell what is wrong with this code? C++
#include <iostream.h>
class Uniform
{
public:
float getUniformDist();
float getUniformMean();
float getUniformVariance();
void setN(float m);
private:
float n;
}; // end of class decleration
// definition of getUniformDist()
// purposse: it will compute the uniform function
// return: float
// parameters: none
float Uniform::getUniformDist()
{
float answer = 0;
answer = 1/n;
return answer;
}
// definition of getUniformMean()
// purposse: it will compute mean for uniform distribution
// return: float
// parameters: none
float Uniform::getUniformMean()
{
float answer = 0;
answer = (n + 1) / 2;
return answer;
}
// definition of getUniformVariance()
// purposse: it will compute variance for uniform distribution
// return: float
// parameters: none
float Uniform::getUniformVariance()
{
float answer = 0;
answer = (n*n - 1) / 12;
return answer;
}
// definition of setN(), public accessor function
// sets n member
void Uniform::setN(float m)
{
n = m;
}
int main()
{
Uniform Uniforms;
cout << "x = 1,2,3,...,n\n";
cout << "Please choose some n.\n";
cout << "n = ";
float n;
cin >> n;
Uniforms.setN(n);
cout << "Uniform fuction is: " << Uniforms.getUniformDist() << "\n";
cout << "Mean: " << Uniforms.getUniformMean() << "\n";
cout << "Variance: " << Uniforms.getUniformVariance() << "\n";
return 0;
} //end main()
*****************************************************************
When i compile it will give me link error. Like this:
Compiling...
one.cpp
Linking...
LINK : fatal error LNK1104: cannot open file "Debug/one.exe"
Error executing link.exe.
one.exe - 1 error(s), 0 warning(s)
The only time i ever get that error is if i still have the exe running in the background.
You may want to restart your computer to ensure the program''s process isn''t still there, then try again.
You may want to restart your computer to ensure the program''s process isn''t still there, then try again.
ok i found out why it gave me hat error, but still i need some explanation on following topic.
As you see above i have private member n in class Uniform. To set n i have accessor function setN(float m). I didnt have accessor function getN() and thats why it didnt work. Now i added getN().
float Uniform::getN()
{
return n;
}
My question now is, why is it necessary to have these getN() ????
It seems like my code will never use it. so why do i have to have it in order to run my code without error? Also why do i need to have setN(float m)? Could i just instead give all my other Uniform class functions parameters and not use setN(float m)?????
I am little confused on this
As you see above i have private member n in class Uniform. To set n i have accessor function setN(float m). I didnt have accessor function getN() and thats why it didnt work. Now i added getN().
float Uniform::getN()
{
return n;
}
My question now is, why is it necessary to have these getN() ????
It seems like my code will never use it. so why do i have to have it in order to run my code without error? Also why do i need to have setN(float m)? Could i just instead give all my other Uniform class functions parameters and not use setN(float m)?????
I am little confused on this
That is not your problem. Adding the getN function would have nothing to do with that error.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement