简体   繁体   English

当我尝试打印矢量的内容时,编译器出现错误?

[英]Compiler is giving errors when I try to print the contents of a vector?

Basically, I have a .h file, in which I have defined a function to generate an element buffer and squashed list of vertices (for OpenGL) given an unoptimized list of vertices. 基本上,我有一个.h文件,在其中我定义了一个函数,用于生成元素缓冲区并压缩给定未优化的顶点列表的顶点列表(对于OpenGL)。 I ran into some problems though, and it turns out that I can't actually access the contents of a vector which I pass to the method. 但是,我遇到了一些问题,结果是我实际上无法访问传递给该方法的向量的内容。 My code is as follows 我的代码如下

#ifndef LEARNOPENGL_COMMON_H
#define LEARNOPENGL_COMMON_H

#include "ContextBase.h" // this includes all the OpenGL stuff
#include "vector"
#include "iostream"

class common {
public:

    template<typename V>
    static bool are_equal(int size, V* v1, V* v2) {
        for (int x = 0; x < size; x++) {
            //if (v1[x] != v2[x]) return false;
        }
        return true;
    }

    template<typename V, typename E>
    static void GenOptimizedArrays(const int vertex_size, std::vector<V>* vertex_source,
                                   std::vector<V>* vertex_out, std::vector<E>* ebo_out) {

        std::vector<V> * vertex_vector = new std::vector<V>();
        std::vector<E> * element_vector = new std::vector<E>();

        std::cout << vertex_source[0] << std::endl;
    }
};

#endif //LEARNOPENGL_COMMON_H

However, my compiler is telling me that trying to print (access?) vertex_source[0] is causing an error- the exact (relevant) error message is 但是,我的编译器告诉我,尝试打印(访问?) vertex_source[0]会导致错误-确切的(相关的)错误消息是

error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}
’ lvalue to ‘std::basic_ostream<char>&&

I tried to search this online but, while I found similar problems, everything just said to use an iterator without explaining why I figured out how from the solutions, but found no good explanation. 我尝试在网上搜索此内容,但是,尽管我发现了类似的问题,但所有内容都只是说要使用迭代器,而没有解释为什么我从解决方案中弄清楚了如何,但是找不到很好的解释。 Can you help on this? 你能帮上忙吗?

std::vector<int> *vec = new vector<int>;
//std::cout<<vec[0];(1)
std::cout<<&vec[0];(2)

If you uncomment (1) and run above snippet you will get almost same error. 如果取消注释(1)并在代码段上方运行,您将得到几乎相同的错误。 Variable vec knows only about its address unless you assign data into its pointer location. 变量vec仅知道其地址,除非您将数据分配到其指针位置。 So when you try to access the data from its pointer location system can't bind it with stream. 因此,当您尝试从指针位置访问数据时,系统无法将其与流绑定。

If you try to print the address as shown in(2) it will print the address of the location variable vec knows. 如果尝试按(2)所示打印地址,它将打印vec知道的位置变量的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM