Today is my second day with <Accerlerated C++>, and so far I've read until chapter three. Below is the code written in the book and as a part of solving the chapter's exercise problem 3-0, I typed and compiled the code. (using vc++ 6.0)
I assumed there would be no problem since the code was written by the authors who are prominent C++ developers.
So I was surprised when the compiler detected seven compilation errors.
I've read the code over and over agained, and looked up MSDN for the errors but I can't seem to figure out what I did wrong.

So here I am, posting my first question asking for help.

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <ios>
#include <string>
#include <vector>

using std::cout;        using std::cin;
using std::endl;        using std::sort;
using std::string;        using std::vector;
using std::streamsize;    using std::setprecision;

int main(void)
{

    cout << "Please enter your name : ";
    string name;
    cin >> name;
    cout << "Hello, "<< name<< "!"<< endl;

    double midterm, final;
    cout << "Midterm : ";    cin>> midterm;
    cout << "Final : ";        cin>> final;

    cout << "Enter all you homework grades, followed by EOF(ctrl+z) : ";

    vector<double> homework;
    double x;
    while (cin >> x)
        homework.push_back(x);

    typedef vector<double>::size_type vec_sz;
    vec_sz size;
    size = homework.size();
    if (size == 0)
    {
        cout<< endl << "You must enter your grades. Please try again." << endl;
        return 1;
    }
    sort(homework.begin(), homework.end());
    vec_sz mid = size / 2;
    double median = (size % 2 == 0) ? (homework[mid] + homework[mid - 1]) / 2 : homework[mid];

    streamsize prec = cout.precision();
    cout << "Your final grade is" << setprecision(3)
         << 0.2 * midterm + 0.4 * final + 0.4 * median << setprecision(prec) << endl;

    return 0;
}

-----------------Configuration: ex3_0 - Win32 Debug--------------------
Compiling...
ex3_0.cpp
error C2653: 'vector<double,class std::allocator<double> >' : is not a class or namespace name
error C2146: syntax error : missing ';' before identifier 'vec_sz'
error C2065: 'vec_sz' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'size'
error C2065: 'size' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'mid'
error C2065: 'mid' : undeclared identifier
Error executing cl.exe.

ex3_0.obj - 7 error(s), 0 warning(s)

These are the error messages I got.
I'm thinking if the first error is solved the rest will be gone.
But, how can I debug error C2653?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I wraped the codes in

and then wraped that in [INLINE][/INLINE] but when I previewed my post it was all green as if it were all comments. Is there anything I can do to change this, and make my code more readible?

Compiled fine for me. It looks like the code you pasted is different than the code that you posted ("vector<double,class std::allocator<double> >" from the error). Another possibility is that your compiler is too old. Visual C++ 6.0 should be trashed, try Visual Studio 2005 Express (free download) or Dev-C++.

>I wraped the codes in and then wraped that in
Well, good for you for reading the rules and figuring out how to use them.

>Is there anything I can do to change this, and make my code more readible?
Here's how the tags work:

Wrap code in [code] and [/code] if you have a section of code, although it won't be colored (all green).

Wrap code in [code=language] and [/code] for syntax coloring. (Here you would use [code=c] or [code=cplusplus]).

Finally, use inline code tags like you mentioned using for code alongside text, like this:

This is my inline code.

[edit] By the way: I like your signature :D

Thanks for the help!
I didn't expect to get a reply this quick.
I imported the project to Dev-C++, tried compiling and it worked!
I guess the old compiler was the problem after all. (Even though I have no idea why the declaration would be a problem for some compilers.)

Thanks again, joeprogrammer and sorry I can't say the same for your signature --- I got absolutely intimidated by the tone while reading. I'm scared of asking questions ever since. It's like waiting for somebody to shout STFW at me.--- though it was very insightful. ;-)

>Thanks for the help!
You're welcome.

>I didn't expect to get a reply this quick.
As long as there's losers like me who are on DaniWeb nearly 6 hours a day, there's going to be quick replies. ;)

>I imported the project to Dev-C++, tried compiling and it worked!
That's good to hear. Now you see why having an up-to-date compiler is essential.

>Even though I have no idea why the declaration would be a problem for some compilers.
I think it has something to do with the typedef statement being within the main() function. I'm not entirely sure though, as I've never run into that problem.

[edit] Actually, I don't think it's a very good idea at all to use typedef inside main(). The reason I've never run into that problem is because I've never done that. I can't find any articles on why not to do it, but again, I haven't had much experience with this keyword, other than for a little bit of programming convenience in my projects.[/edit]

>sorry I can't say the same for your signature --- I got absolutely intimidated by the tone while reading.
Heh, don't worry about it my friend. That document is more aimed at the massive amounts of newbies we get here who ask for homework help without showing any effort, use leet speak, and in general don't care to read the rules. You actually asked a very good question, and I appreciate that you took the time to do so. Thank you.

I had the same problem with Visual C++ 6.0 (trying the same example; same book)

explicitly stating the namespace like below works, I would love to know why though (shouldn't "using std::vector;" have take care of this?)

typedef std::vector<double>::size_type vec_sz;

--
Yiannis

This question has already been answered. Start a new discussion instead.