c++ - 如何将点添加到结构类型的 vector

标签 c++ vector stl

我正在尝试将点(顶点)添加到结构类型的 vector 中。我是初学者,我知道我可以使用 push_back。但我不断收到三个错误:

  • 没有合适的默认构造函数可用
  • '.push_back' 的左侧必须有类/结构/union
  • 表达式必须有类类型。

我做错了什么? 这是我的代码...

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <math.h>
using namespace std;

struct Points
{
    int x, y;
    Points(int paramx, int paramy) : x(paramx), y(paramy) {}  
}p1,p2;

vector <Points> pointes();

void addPoint(int a, int b);
void directionPoint(Points p1, Points p2);

int main()
{
    return 0;
}

void addPoint(int x, int y)
{
    pointes.push_back(Points(x, y));    
}

void directionPoint(Points p1, Points p2)
{  
    if ((p1.x*p2.y - p2.x*p1.y) > 0)
    {
        cout << "direction is anticlockwise" << endl;
    }
    else
        cout << "direction is clockwise" << endl;
}

最佳答案

错误没有合适的默认构造函数可用是由您的代码引起的

} p1,p2;

这可以通过在结构中创建适当的构造函数、在不需要时删除这些值或使用现有构造函数来纠正:

} p1(0,0),p2(0,0);

The left of '.push_back' must have class/struct/union and expression must have class type错误是由

引起的
vector <Points> pointes();

要更正它,请删除括号:

vector <Points> pointes;

关于c++ - 如何将点添加到结构类型的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26952427/

相关文章:

c++ - STL 容器、SBO 和自定义分配器冲突

java - 最后没有显示员工摘要的问题

c++ - 从 BoostCon 谈话中重载取消引用运算符的奇怪方式

c++ - 将 vector 中的对象指针移动到不同的 vector

C++ 查找并保存 vector 中的重复项

c++ - 无序映射比 C++ 中的映射慢吗?

c++ - std::list<char> 列表类型为 (char * data, int length)

c++ - 数组中的最小差异

c++ - 如何不在 OpenGL 中使用着色器覆盖顶点颜色?

java - 使用 ActivePivot 进行稀疏 vector 聚合