[英]c++ which is of non-class type error
for the start keep in mind I am a complete rookie in c++. 首先请记住,我是C ++中的一名新手。 I have read a lot about templates and strings today, but I cant figure couple of things out.
今天,我已经阅读了很多有关模板和字符串的信息,但是我无法弄清其中的几件事。 When I create a point in my test class with point<2> or any other value.
当我在测试类中使用point <2>或任何其他值创建一个点时。 I have an error: request for member 'tostring' in 'v2' which is of non-class type... 1. Why do I get that error?
我有一个错误:请求“ v2”中属于非类类型的成员“ tostring” ... 1.为什么会出现该错误? 2. How can make Point() = default;
2.如何使Point()= default; to make every coordinate value 0.0;
将每个坐标值设为0.0; Like if I have Point<1> it would be (0.0), Point<2> would be (0.0, 0.0) and so on:.
就像我的Point <1>是(0.0),Point <2>是(0.0,0.0),依此类推:
#ifndef POINT_H
#define POINT_H
#include <iostream>
#include <list>
#include <sstream>
#include <string>
using std::stringstream;
#include <cmath>
using namespace std;
template<unsigned short n>
class Point {
public:
list <float> coords = {0.0};
Point <n>() = default;
Point <n>(list<float> coords){
this-> coords=coords;
}
string toString(){
string sone;
ostringstream ss;
sone.append("(");
auto it3= coords.begin();
while ((it3) != coords.end()){
ss << (*it3);
sone.append(ss.str());
ss.str("");
}
sone.append(")");
return sone;
}
float distanceFrom (Point <n> v){
float s=0;
list<float> coords;
auto it1= coords.begin();
auto it2= v.coords.begin();
while ((it1) != coords.end()){
s+=(*it1 -*it2)*(*it1-*it2);
it1++;
it2++;
}
return sqrt(s);
}
friend std::ostream& operator <<(std::ostream& out, const Point<n>& v)
{
out << "("<<"Test"<<")";
return out;
}
};
#endif
Increment iterator in a method toString and it works: 在方法toString中增加迭代器,它可以工作:
string toString(){
string sone;
ostringstream ss;
sone.append("(");
auto it3= coords.begin();
while ((it3) != coords.end()){
ss << (*it3);
sone.append(ss.str());
ss.str("");
++it3;
^^^
}
sone.append(")");
return sone;
}
The note about templates: 关于模板的注意事项:
inside the class body you don't need to say Point<n>
, just Point
: 在类体内,您无需说
Point<n>
,而只需说Point
:
Point () = default;
Point ( list<float> coords){ //...}
To print Point you have to make toString
method const
: 要打印Point,您必须使
toString
方法const
:
string toString() const { //... }
to enable call on a const Point. 启用对const Point的调用。 Now you can use it in
operator<<
: 现在您可以在
operator<<
使用它:
friend std::ostream& operator <<(std::ostream& out, const Point<n>& v) {
out << v.toString();
return out;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.