我正在尝试将点(顶点)添加到结构类型的 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/