【问题标题】:how to bypass forward declarations如何绕过前向声明
【发布时间】:2014-04-24 22:04:25
【问题描述】:

这个程序可以工作,但是如果没有 UList.H 中的前向声明,我如何使这个程序工作。如果我删除它们,它会给我错误。如果您想查看这些错误,请在此处查看我的其他问题https://stackoverflow.com/questions/23278943/how-to-implement-sort-functions-using-vectors

UList.h

#ifndef PROJ_ULIST_H
#define PROJ_ULIST_H

#include <iostream>
#include <vector>

//forward declarations of UList and friended functions
template<class T> class UList;

template<class T>
std::ostream& operator<<(std::ostream&, const UList<T>&);

template<class T>
void sort(UList<T>&);

template <class T>
class UList{
public:
    UList(size_t=10);
    void insert(const T&);
    bool erase(const T&);
    bool find(const T&);
    size_t size() const;
    bool empty() const;
    friend void sort<T>(UList<T>&);
    friend std::ostream& operator << <T>(std::ostream&, const UList<T>&);

protected:
    std::vector<T> items;
};


template <class T>
UList<T>::UList(size_t size){
    std::vector<T> items(size);
}


template <class T>
bool UList<T>::find(const T& element){
    bool found=false;
    size_t index=0;
    while(index<items.size()){
        if(items.at(index)==element)
            found=true;
        index++;
    }
    return found;
}


template <class T>
size_t UList<T>::size() const{
    return items.size();
}


template <class T>
bool UList<T>::empty() const{
    return items.empty();
}

template<class T>
std::ostream& operator << (std::ostream& out, const UList<T>& List){
    if(List.items.empty())
        out<<"list is empty."<<std::endl;
    else{
        for(size_t index=0;index<List.items.size();index++){
            out<<List.items.at(index);
                if(index<List.items.size()-1)
                    out<<" ";
        }
    }
    return out;
}


#endif

sortBS.h

#ifndef PROJ_SORTBS_H
#define PROJ_SORTBS_H
#include <algorithm>
#include <vector>
#include "UList.h"

template <class T>
void sort(UList<T>& List){
    std::vector<T>& items=List.items;
    size_t len=items.size();
    bool swapped = true;

    while((len--!=0)&& swapped){
        swapped=false;
        for(size_t i=0; i<len;++i){
            if(items[i+1]<items[i]){
                std::swap(items[i+1], items[i]);
                swapped=true;
            }    
        }
    }
}

【问题讨论】:

  • 什么错误?友元声明应该足够了,但它们会将这些函数的查找限制为 ADL,这可能会导致您在代码中出现我们看不到的问题。你可以(IIRC)稍后重新声明,after UList,来解决这个问题。我只能猜测,因为这里没有具体的问题。

标签: c++ sorting vector forward-declaration


【解决方案1】:

有时前向声明是必要的。如果将operator&lt;&lt;sort 放在UList 的定义之上,则可以只使用UList 的前向声明。我不确定为什么sort 无论如何都在不同的头文件中。如果您希望按原样组织代码,则前向声明是必要的。

【讨论】:

  • sort 在不同的头文件中,因为我们正在为不同的排序函数创建多个头文件,我并不是要粗鲁,但我的教授不这么认为。他告诉我他们没有必要并且可以修复它,但不会告诉我如何。
  • 一点也不粗鲁。也许他在这种情况下是对的。 (虽然如果他暗示他们永远不需要,我不相信这是真的。)希望其他人(或你!)会想出一个解决方案并发布它。 :)
  • 我的教授没有前向声明,他对sortoperator &lt;&lt; 的声明都在UList 类中
  • 我不知道该告诉你什么。我保证你的教授比这里的人更了解 C++,但没有人回答你的问题。我不确定你说它们“都在 UList 类中[声明]”是什么意思。它们是不是朋友功能?
  • 我应该补充一点,编译器可能会有所不同。你的教授使用的编译器和你一样吗?
猜你喜欢
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
  • 2010-10-16
  • 2021-05-15
  • 1970-01-01
相关资源
最近更新 更多