小编One*_*ser的帖子

在Windows上运行但在linux上运行的迭代器

我目前正在编写模板矩阵.它包含一个vector<vector<T>>名为mat和cols和rows的val,它们包含行数和列数.我试图构建一个迭代器,并发现我无法为vector向量构建迭代器函数.因为我的代码的其余部分已经写好了,所以我添加了一个matrixToVector函数,它将我vector<vector<T>>转向vector<T>(我知道这不是最佳选择,但它仅适用于大学练习).在我的Windows笔记本电脑上很好,但在Linux计算机实验室中,前两个迭代器的数量总是一个非常大的随机数,然后是0,然后其余的数字都很好.这是代码:

    /**
     * turns the 2d mat vecor to 1d vector.
     */
    vector<T> matrixToVector()
    {
        vector<T> v;
        for(unsigned int i = 0 ; i < rowsNum; i++)
        {
            for(unsigned int j = 0; j < colsNum; j++)
            {
                v.push_back(mat[i][j]);
            }
        }
        return v;

    }

    /**
     * iterator
     */
    typedef typename std::vector<T>::const_iterator const_iterator;


    /**
     * return the end of the iterator.
     */
    const_iterator end()
    {
        return matrixToVector().end();
    }

    /**
     * return the begining of the …
Run Code Online (Sandbox Code Playgroud)

c++ linux iterator

1
推荐指数
1
解决办法
123
查看次数

C++包含并重新定义了类错误

我目前正在编写一个程序,根据不同的参数搜索歌曲.在我的系统中有两种类型的歌曲:歌词和乐器.因为我需要将它们都放在1个向量中,所以我有一个歌曲类和一个LyricsSong&InstrumentalSong子类.

所以我有一个Song.h文件:

#include <stdio.h>
#include <iostream>
#include <string>


class Song
{
public:
    std::string title;
    virtual void print();
    virtual void printSong(std::string query);
};
Run Code Online (Sandbox Code Playgroud)

还有乐器和歌词子类,它们以这种方式定义:

class LyricsSong : public Song
class InstrumentalSong : public Song
Run Code Online (Sandbox Code Playgroud)

两者都包括Song.h,在这两个类中,类只在头文件中定义.

当我尝试运行另一个使用这两个子类的文件时,包括:

#include "LyricsSong.h"
#include "InstrumentalSong.h"
Run Code Online (Sandbox Code Playgroud)

(显然更多的cpp库),我得到以下编译错误:

In file included from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/InstrumentalSong.h:16:0,
                 from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/songsParser.cpp:26:
/cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/Song.h:6:7: error: redefinition of 'class Song'
 class Song
       ^
In file included from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/LyricsSong.h:15:0,
                 from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/songsParser.cpp:25:
/cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/Song.h:6:7: error: previous definition of 'class Song'
 class Song
       ^
Run Code Online (Sandbox Code Playgroud)

什么时候: …

c++ polymorphism inheritance multiple-inheritance include

0
推荐指数
2
解决办法
4255
查看次数