简体   繁体   English

将Windows代码移植到Mac

[英]Porting Windows code to Mac

I'm migrating C++ code from Windows to Mac. 我正在将C ++代码从Windows迁移到Mac。 I choose OS X Command line Tool with language C++. 我选择使用C ++语言的OS X命令行工具。

Here is the code snippet: 这是代码片段:

    #ifndef __PRIORITYQUEUE_H__
    #define __PRIORITYQUEUE_H__
    #include <queue>

    template <typename T, class Container = std::vector<T>, class Compare = std::less<typename Container::value_type>>
    class PriorityQueue : public std::priority_queue<T, Container, Compare>
    {
    public:
        PriorityQueue() {}
        PriorityQueue(const PriorityQueue& pq) {priority_queue(pq);}   // line:13
        explicit PriorityQueue(const Compare& compare) {priority_queue(compare);}
        PriorityQueue(const Compare& compare, const Container& container) {priority_queue(compare, container);}
        template<class Iter> PriorityQueue(Iter first, Iter last) {priority_queue(first, last);}
        template<class Iter> PriorityQueue(Iter first, Iter last, const Compare& compare) {priority_queue(first, last, compare);}
        template<class Iter> PriorityQueue(Iter first, Iter last, const Compare& compare, const Container& container) {priority_queue(first, last, compare, container);}
    ......
    #endif

And there are other files will include this header file. 并且还有其他文件将包含此头文件。 When I compile it, the following error happens: 当我编译它时,发生以下错误:

  1. /Users/yingyhe/Projects/SASCode/SASPRLib/PriorityQueue.h:13:53: Unknown type name 'priority_queue'; /Users/yingyhe/Projects/SASCode/SASPRLib/PriorityQueue.h:13:53:未知类型名称“ priority_queue”; did you mean 'PriorityQueue'? 您是说“ PriorityQueue”吗?
  2. /Users/yingyhe/Projects/SASCode/SASPRLib/PriorityQueue.h:13:68: Redefinition of 'pq' /Users/yingyhe/Projects/SASCode/SASPRLib/PriorityQueue.h:13:68:重新定义“ pq”

It seems this header file isincluded multiple times, but I've used #ifndef - #define - #endif to avoid that. 似乎多次包含了此头文件,但我使用了#ifndef-#define-#endif来避免这种情况。

Then I take this header file to a new project, and no other file include it, and it can compile successfully. 然后,我将此头文件带到一个新项目中,并且没有其他文件包含它,并且它可以成功编译。 It's wired, anyone can tell me why? 它已连线,任何人都可以告诉我为什么? Many Thanks!!! 非常感谢!!!

You forgot the std:: prefix. 您忘记了std::前缀。

I suspect that you usually include this file where you're also using namespace std; 我怀疑您通常在using namespace std;位置包含此文件using namespace std; prior to its inclusion. 在加入之前。

But there are more problems. 但是还有更多的问题。

First of all, the standard library containers are not made for use as base classes. 首先,没有将标准库容器用作基类。
Don't do it. 不要这样

Base classes are initialised in the initialiser list , not in the constructor body like in eg Java: 基类在初始化器列表初始化 ,而不是在构造器主体中初始化 ,例如在Java中:

PriorityQueue(const PriorityQueue& pq) : std::priority_queue(pq) {} 

You get the redefinition error because you're declaring a local variable with the same name as the parameter. 您得到重新定义错误,因为您声明的局部变量的名称与参数相同。

In the body of a function, 在功能主体中,

std::priority_queue(pq);

is equivalent to 相当于

std::priority_queue pq;

This follows the "if it could be a declaration, it is a declaration" rule. 这遵循“如果可能是声明,那就是声明”规则。

If no other file includes the header, it won't be compiled at all, which it why it works in that case. 如果没有其他文件包含标头,则根本不会编译它,这就是在这种情况下起作用的原因。
It will be compiled once for each source file it is included in, which is as expected - "include guards" guard against multiple inclusions in a translation unit , and each translation unit is compiled separately. 它将为包含在其中的每个源文件编译一次,这是预期的-“ include guards”防止翻译单元中出现多个包含内容,并且每个翻译单元分别进行编译。

I changed line 13 to this: 我将第13行更改为:

`PriorityQueue(const PriorityQueue& pq) : priority_queue<T, Container, Compare>(pq) {}`

and it's pass the compile 并且通过了编译

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

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