C++ 访问类的元素

C++ 访问类的元素,c++,class,vector,C++,Class,Vector,我试图创建一个类“三角形”,它被表示为“顶点”类型的向量,“顶点”是由“x”和“y”组成的结构,它们是double类型。我需要在“triangle”中有一个成员函数,它使用herons公式返回面积,但我不知道如何访问三角形中每个元素的单独x和y分量。这是到目前为止我的代码,麻烦来自第三段代码-triangle.cpp文件 顶点h: #ifndef VERTEX_H #define VERTEX_H #include <iostream> struct vertex { d

我试图创建一个类“三角形”,它被表示为“顶点”类型的向量,“顶点”是由“x”和“y”组成的结构,它们是double类型。我需要在“triangle”中有一个成员函数,它使用herons公式返回面积,但我不知道如何访问三角形中每个元素的单独x和y分量。这是到目前为止我的代码,麻烦来自第三段代码-triangle.cpp文件

顶点h:

#ifndef VERTEX_H
#define VERTEX_H

#include <iostream>

struct vertex
{
    double x, y;

    vertex(double ix = 0.0, double iy = 0.0)
    {
        x = ix;
        y = iy;
    }
};

#endif // VERTEX_H
#如果没有顶点#
#定义顶点
#包括
结构顶点
{
双x,y;
顶点(双ix=0.0,双iy=0.0)
{
x=ix;
y=iy;
}
};
#endif//顶点
三角形h:

#ifndef TRIANGLE_H
#define TRIANGLE_H

#include <iostream>
#include <vector>
#include "vertex.h"

class triangle
{
public:
    triangle(vertex iv0 = vertex(), vertex iv1 = vertex(), vertex iv2 = vertex());
    // pre:
    // post: empty triangle

    triangle(const triangle & source);
    // pre:
    // post: triangle created and initialized to given triangle source

    vertex operator[](size_t i) const;
    // pre: 0 <= i < 3
    // post: return vertex i in this triangle

    double area() const;
    //pre:
    //post: returns area of triangle

private:
    std::vector<vertex> v;
};

std::ostream & operator << (std::ostream & os, const triangle & t);
std::istream & operator >> (std::istream & is, triangle & t);
#endif // TRIANGLE_H
#如果是三角形#
#定义三角形
#包括
#包括
#包括“vertex.h”
阶级三角
{
公众:
三角形(顶点iv0=顶点(),顶点iv1=顶点(),顶点iv2=顶点());
//前:
//帖子:空三角形
三角形(常数三角形和源);
//前:
//post:创建三角形并初始化为给定三角形源
顶点操作符[](大小)常量;
//前置:0(标准::istream&is、triangle&t);
#endif//三角形
triangle.cpp源文件:

#include <cassert>
#include <vector>
#include <cmath>
#include "triangle.h"

triangle::triangle(vertex iv0, vertex iv1, vertex iv2)
{
    v = std::vector<vertex> ();
    v[0] = iv0;
    v[1] = iv1;
    v[2] = iv2;
}

triangle::triangle(const triangle &source)
{
    v = source.v;
}

vertex triangle::operator[] (std::size_t i) const
{
    assert(i < v.size());
    return v[i];
}
 //HERE IS THE PROBLEM
double triangle::area() const
{
    int a, b, c;
    double s;
    //need to use something like a = (v[0].y-v[1].y)/(v[0].x-v[1].x) to calculate area,
    //   this syntax is wrong however^^^
    s = (a+b+c)/2;
    return sqrt(s*(s-a)*(s-b)*(s-c));
}

std::ostream & operator << (std::ostream & os, const triangle & t)
{
    for (size_t i = 0; i < 3; ++i)
        os << t[i] << " ";
    return os;
}

std::istream & operator >> (std::istream & is, triangle & t)
{
    std::size_t n;
    vertex vx;

    is >> n;
    for (size_t i = 0; i < n; ++i)
    {
        is >> vx.x >> vx.y;
    }
    return is;
}
#包括
#包括
#包括
#包括“triangle.h”
三角形::三角形(顶点iv0、顶点iv1、顶点iv2)
{
v=std::vector();
v[0]=iv0;
v[1]=iv1;
v[2]=iv2;
}
三角形::三角形(常量三角形和源)
{
v=源。v;
}
顶点三角形::运算符[](标准::大小\u t i)常数
{
断言(in;
对于(尺寸i=0;i>vx.x>>vx.y;
}
回报是;
}

访问向量的方式没有问题(
v[0].x
etc是正确的),但初始化向量的方式存在问题:

triangle::triangle(vertex iv0, vertex iv1, vertex iv2)
{
    v = std::vector<vertex> ();
    v[0] = iv0;
    v[1] = iv1;
    v[2] = iv2;
}

确切的错误是什么?没有错误,只需要帮助定义三角形的边长,您不想对a、b和c使用整数
// Using push_back
triangle::triangle(vertex iv0, vertex iv1, vertex iv2)
{
    v.reserve(3);
    v.push_back(iv0);
    v.push_back(iv1);
    v.push_back(iv2);
}

// Preallocating a vector size before modifying elements
triangle::triangle(vertex iv0, vertex iv1, vertex iv2)
    : v(3)
{
    v[0] = iv0;
    v[1] = iv1;
    v[2] = iv2;
}