【问题标题】:Error when trying to add data to vector inside structure尝试将数据添加到结构内的向量时出错
【发布时间】:2017-05-20 20:58:13
【问题描述】:

我正在尝试将 .txt 文件中的一些数据添加到结构内的向量中,但该向量无法编译。代码如下所示。

#include <iostream>
#include <fstream>
#include <typeinfo>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

typedef struct
{
    vector<double> xAxis[100];
    vector<double> yAxis[100];
} AxisValues;

void startup()
{
    cout << "Numerical Integrator - calculating area under a curve.\n\n";
    cout << "Enter the name of the file which contains data points (x and y coordinates) that form the curve:\n";
}

void inputVal()
{
    AxisValues aValues;
    int n1 = 0, n2 = 0, n3 = 0, n4 = 0, n5 = 0, n6 = 0, n7 = 0, n8 = 0;
    string fileName;
    ifstream infile;

    cin >> fileName; //enter curve_s1.txt here

    infile.open(fileName);
    if (infile.fail()) { cout << "ERROR: FILE NOT DETECTED\n\n"; }

    for (n1 = 0; n1 < 10; n1++)
    {
        infile >> aValues.xAxis[n2];
        n2++;
        for (n3 = 0; n3 < 10; n3++);
        {
            infile >> aValues.yAxis[n4];
            n4++;
        }
    }

    cout << "x  y\n";
    for (n5 = 0; n5 < 10; n5++)
    {
        cout << aValues.xAxis[n6] << "  ";
        n6++;
        for (n7 = 0; n7 < 10; n7++);
        {
            cout << aValues.yAxis[n8] << "\n";
            n8++;
        }
    }
}

double trapezoidalRule(const double &x1, const double &x2, const double &y1, const double &y2)
{
    double result = 0;
    result = (x2 - x1)*((y1 + y2) / 2);
    return result;
}

int main()
{
    startup();
    inputVal();
    //cout << trapezoidalRule(4, 5, 2, 3) << "\n";

    system("pause");
    return 0;
}

我收到以下错误。

E0349 没有操作符“>>”匹配这些操作数 E0349 没有操作符“>>” 匹配这些操作数 E0349 no operator "

C2679 二进制“>>”:未找到采用右手操作数的运算符 'std::vector>' 类型的(或者没有 可接受的转换)

C2679 二进制“>>”:未找到采用右手操作数的运算符 'std::vector>' 类型的(或者没有 可接受的转换)

C2679 二进制 '' 类型的(或者没有 可接受的转换)

错误 C2679 二进制 '' 类型的操作数(或者有 没有可接受的转换)

【问题讨论】:

  • 你的意思是有一个包含 100 个向量的数组吗?
  • vector&lt;double&gt; xAxis[100]; 定义了一个向量数组,而不是一个大小为 100 的向量。您确定这是您想要的吗?
  • 哦,好吧,对不起,我对 c++11 一无所知,请删除我的评论。谢谢
  • 在尝试如此复杂的事情之前,您必须尝试更简单的练习——例如将数字放入向量中。

标签: c++ vector structure


【解决方案1】:

如果你想创建一个大小为 100 的向量,你应该输入:

vector <int> vecName(100);

在这种情况下,您可以使用 cin 将数字从流添加到向量,如下所示(您在代码中这样做):

cin>>vecName[i];

但是如果你想要有 100 个向量,你应该输入(你做了):

vector <int> vecName[100];

您可以像这样通过临时变量添加 int:

int temp;
cin>>temp;
vecName[i].push_back(temp);

您的错误来自您将整数发送到向量而不是整数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多