I keep getting error code C2039
- I am doing a class assignment and keep getting C2039 'setprecision' is not a member of 'std'.
I am using #include <iomanip> I have checked spelling, rearranged things etc. I cannot get the program to comply with out this error as well as C2873 and C2871. All have to do with 'setprecision'. These are the only errors I get. Help please.
Thanks
Regina
#include
<iostream>
using
std::cin;
using
std::cout;
using
std::endl;
using
std::fixed;
#include
<iomanip> // parameterized stream manipulators
using
std::setprecision; // sets numeric output precision
#include
"Program3_Wages.h" // include definition of class Wages from Wages.h
// function to calculate wages
void
Wages::calculateWages()
{
double hours; // total hours worked
double rate; // hourly pay rate
double salary; // gross pay
// processing phase
// get first employee's hours worked
cout <<
"Enter hours worked (-1 to end): ";
cin >> hours;
// set floating-point number format
cout << fixed << setprecision( 2 );
// place your while loop here
// loop until sentinel value read from user
while ( hours != -1 ) // while hours are not -1
{
// get double rate
cout <<
"Enter hourly pay rate: "; // prompt for input
cin >> rate;
// input next hourly pay rate
salary = hours * rate;
// determines gross pay
}
// end while
}
// end function calculateWages
Answers
- To turn off precompiled headers for the project, right click on the Project
name in the Solution Explorer window. Select "Properties". Under
Configuration Properties->C/C++->Precompiled Headers
click on "Create/Use Precompiled Headers" and from the dropdown box
select "Not Using Precompiled Headers"
Click on "Apply/OK" and rebuild.
If that doesn't help, try this:
Open the "Build" menu, click on "Clean Solution".
Then click on "Rebuild Solution".
If the problem persists, what happens if you try this?
Omit the using std::setprecision; and put the std:: on the manipulator:
cout << fixed << std::setprecision( 2 );
If that doesn't work remove that line and try this instead:
cout << fixed;
cout.precision(2);
- Wayne- Marked As Answer byNancy ShaoMSFT, ModeratorMonday, November 09, 2009 1:59 AM
All Replies
- Post an example that actually reproduces the error and that we can try
to compile. There are no apparent problems in your snippet up to and
including the using std::setprecision statement.
Also try turning off precompiled headers for this project and rebuilding.
Also be sure to specify *which* compiler and *which* version you're using,
including any service packs or patches installed.
- Wayne - This is the entire code that I am using. I am using Visual C++ 2008 express edition.
Hear is the header file:
// Program #3 Assignment #4 Object Interface: Program3_Wages.h
// Definition of class Wages that calculates wages.
// Member functions are defined in Program3_Wages.cpp
// (Interface separate from implementation!)
// Wages class definition
class
Wages
{
public
:
void calculateWages(); // function to calculate wages
// Note: Expecting no arguments (values) coming in
// and no value returned to caller
};
// end class Wages
Here is the other source file for this assignment:
/ Program #3 Assignment #4 Solution: Program3_Client.cpp
// Create Wages object and invoke its calculateWages function.
#include "Program3_Wages.h" // include definition of class Wages from Program3_Wages.h
int
main()
{
Wages myWages;
// create Wages object myWages
myWages.calculateWages();
// call it's calculateWages function (Note: No arguments sent, and no value to be returned!)
return 0; // indicate program ended successfully
}
// end main
The piece above is my part of the assignment. I tried running it without the header file and the second source file and still got the same errors. - Quote>I am using Visual C++ 2008 express edition.
With or without SP1?
There are no problems in the code you posted which would cause the errors
you reported. Did you try my previous suggestion?
- Wayne - The code you submitted compiles without errors. Please use the "Insert Code Block" function on the toolbar (furthest right) to include code so it does not get weirdly formatted. This would imply that either your source file has unprintable characters, your project configuration has a problem, or the reformatting of your inserted code eliminated the syntax error.
The version I was able to re-format from your message that compiles looks as follows:
#include <iostream> using std::cin; using std::cout; using std::endl; using std::fixed; #include <iomanip> // parameterized stream manipulators using std::setprecision; // sets numeric output precision #include "Program3_Wages.h" // include definition of class Wages from Wages.h
If this matches what does not compile in your environment, try a Rebuild instead of a build. Sometimes pre-compiled headers create strange problems.
If that doesn't work, the clue might lie in the other two errors you reported.
- C2871 - The name given in the using declaration does not exist.
- C2873 - A using definition is missing the "namespace" keyword.
If that doesn't work, create a new empty CPP Win32 Console Application project using just the default settings and compile the following simple HelloWorld sample to test your io streams.
#include <iostream> using std::cout; using std::endl; void main() { cout << "Hello World!" << endl; }
If that works, delete the HelloWorld sample source file, copy over just the CPP and H files for the Wages class, and compile that. If that works, the problem is with your original project configuration.
If none of that works, or you get other errors, or the errors are not for those lines, reply with a copy of the output window so we can see precisely what errors you are getting and where they are occurring.
I hope that helps. - I do not know if it is with or without SP1. I am a beginner and do not know how to tell. I could not figure out how to turn off the header so I tried to run it without the header and the other source file and still got the same errors.
- To turn off precompiled headers for the project, right click on the Project
name in the Solution Explorer window. Select "Properties". Under
Configuration Properties->C/C++->Precompiled Headers
click on "Create/Use Precompiled Headers" and from the dropdown box
select "Not Using Precompiled Headers"
Click on "Apply/OK" and rebuild.
If that doesn't help, try this:
Open the "Build" menu, click on "Clean Solution".
Then click on "Rebuild Solution".
If the problem persists, what happens if you try this?
Omit the using std::setprecision; and put the std:: on the manipulator:
cout << fixed << std::setprecision( 2 );
If that doesn't work remove that line and try this instead:
cout << fixed;
cout.precision(2);
- Wayne- Marked As Answer byNancy ShaoMSFT, ModeratorMonday, November 09, 2009 1:59 AM
- Quote>I do not know if it is with or without SP1.
Quote>I am a beginner and do not know how to tell.
Click on the Help menu. Click on the "About ..." item.
Read what it says for "Version ..."
- Wayne - Thanks the cout.precision(2); got rid of the errors.
- Version 9.0.30729.1SP
- Hi Reginat1,
Have you tried Llelan D.'s suggestion? Create a new Project and copy your code to it. Or you can take a simple test with std::setprecision( 2 );, to see whether this issue will occur. If you still can't solve this issue, could you please post a code snippet to reproduce this issue?
Best Regards,
Nancy
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

