Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

c++ - No operator "=" matches these operands

So this must be something really silly, but I'm getting an error with this code.

What could be going wrong, the operands <, > also don't work. Does one use vectors differently? When I try y.at(1) = 10; it says expression must have class type...?

#include "stdafx.h"
#include <iostream>
#include "time.h"
#include <vector>


int main()
{
    using namespace std;
    const long long l = 100000;

    vector <int> y[l];
    long long x[l];

    y[0] = 10; // Test statement results in Error.

//for (long i = 0;i < l;i++) {
    //  y.at(i) = i;//rand() % 100;
    //  x[i] = rand() % 100;
    //}



    clock_t t = clock();

    for (long long i = 0;i < l;i++) {
        long long r;
        r = y[i] ^ ((x[i]^y[i]) & -(x[i] < y[i]));
        /*if (x[i] < y[i]) {
            r = x[i];
        }
        else {
            r = y[i];
        }*/

    }

    t = clock() - t;

    printf("It took %d ms ", t);

return 0;

}

For context I'm trying to test for run times. Was using std::array at first, but it seems like that doesn't work with large array sizes, so I decided to try out vectors.

Used http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm as a reference, but it seems like although I'm doing the exact same thing, something is not working.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a quite common typo, writing

std::vector<int> y[10];

declares an array of 10 empty vectors. To have a vector of 10 elements you need

std::vector<int> y(10);

instead.

You're not alone in thinking that the error message when making this mistake is somewhat cryptic... this is an area in which unfortunately C++ is lacking (not sure now, but I remember that there were companies making a living on just deciphering C++ error messages from VC++).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...