Ask a questionAsk a question
 

AnswerI keep getting error code C2039

  • Sunday, November 01, 2009 3:13 PMreginat1 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Sunday, November 01, 2009 11:06 PMWayneAKing Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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

All Replies

  • Sunday, November 01, 2009 5:47 PMWayneAKing Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Sunday, November 01, 2009 6:40 PMreginat1 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.

  • Sunday, November 01, 2009 10:07 PMWayneAKing Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Sunday, November 01, 2009 10:29 PMLlelan D. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    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.
    1. C2871 - The name given in the using declaration does not exist.
    2. C2873 - A using definition is missing the "namespace" keyword.
    If these errors are occurring on the std::setprecision using declaration line, then the compiler is not recognizing "std" as a valid namespace name. You might have some unprintable characters in that line and would need to delete and re-type the entire line. If the errors occur in the other using declarations, you might have other unprintable characters in the file, a corrupt iostream.h file, or a mis-configured project. Try re-typing the entire section or file from scratch.

    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.
  • Sunday, November 01, 2009 10:42 PMreginat1 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Sunday, November 01, 2009 11:06 PMWayneAKing Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
  • Sunday, November 01, 2009 11:11 PMWayneAKing Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Monday, November 02, 2009 12:11 AMreginat1 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks the cout.precision(2); got rid of the errors.
  • Monday, November 02, 2009 12:12 AMreginat1 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Version 9.0.30729.1SP
  • Tuesday, November 03, 2009 2:51 AMNancy ShaoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.