简体   繁体   English

非类类型错误的C ++

[英]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;
        }

compiled example 编译的例子


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.

相关问题 非类类型的成员 .. 的错误请求 - C++ - error request for member .. in .. which is of non-class type - C++ 非成员类型的C ++成员请求 - C++ request for member, which is of non-class type C ++二叉树错误:请求(Y)中非类类型(Z)的成员(X) - C++ Binary Tree error: request for member (X) in (Y) which is of non-class type (Z) C ++:对“ f”中非成员类型“ Foo *”的成员“ Foo”的错误请求 - C++: Error request for member 'Foo' in 'f', which is of non-class type 'Foo*' 对B中非类类型C的成员A的c ++请求 - c++ request for member `A' in `B', which is of non-class type `C' c++:请求“dt2”中的成员“get_date”,它属于非类类型“DateTime (*)() - c++: request for member ‘get_date’ in ‘dt2’, which is of non-class type ‘DateTime (*)() 链表函数c ++“哪个是非类类型&#39;int&#39;”? - linked list function c++ “which is of non-class type 'int'”? 对(blank)中的成员(blank)的C ++请求,在调用方法时属于非类类型(blank) - C++ request for member (blank) in (blank) , which is of non-class type (blank) when calling method C ++:奇怪的出现“请求Y成员X的非类型Z” - C++: bizarre occurrence of “Request for member X of Y which is of non-class type Z” 非类类型&#39;char&#39;的成员打印错误请求 - Error request for member print which is of non-class type 'char'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM