小编Rob*_*aru的帖子

MacOS 上的 C++:显示日期和时间问题

我买了一台 MacBook Pro,过去两天我一直在使用 MacOS。我一直在尝试编写此 C++ 代码,该代码输出使用chrono和ctime库的日期和时间。这段代码在我的 Windows 机器和 CentOS7 服务器上运行良好。但是,在我的 MacBook Pro 上它无法编译。这是我尝试使用 G++ 编译时收到的错误消息:

main.cpp:19:61: error: no viable conversion from 'time_point<std::__1::chrono::steady_clock,
      duration<[...], ratio<[...], 1000000000>>>' to 'const
      time_point<std::__1::chrono::system_clock, duration<[...], ratio<[...], 1000000>>>'
    std::time_t date = std::chrono::system_clock::to_time_t(now);
                                                            ^~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/chrono:1340:28: note: candidate constructor
      (the implicit copy constructor) not viable: no known conversion from
      'std::__1::chrono::time_point<std::__1::chrono::steady_clock, std::__1::chrono::duration<long long,
      std::__1::ratio<1, 1000000000> > >' to 'const
      std::__1::chrono::time_point<std::__1::chrono::system_clock, std::__1::chrono::duration<long long,
      std::__1::ratio<1, 1000000> > > &' for 1st argument
class _LIBCPP_TEMPLATE_VIS time_point
                           ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/chrono:1340:28: note: …
Run Code Online (Sandbox Code Playgroud)

c++ macos clang++ c++14

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

C++ 访问静态 constexpr 数组

我试图从另一个类中声明的数组中获取一些值。该数组具有固定长度和常量元素(我将 100% 从不修改其值,所以这就是我将其设为常量的原因)。

但是,当我尝试访问main函数中的第一个元素时,出现编译错误:

basavyr@Roberts-MacBook-Pro src % g++ -std=c++11 main.cc
Undefined symbols for architecture x86_64:
  "Vectors::vec1", referenced from:
      _main in main-c29f22.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在使用 clang(最新版本)在 macOS Catalina 上进行编译。

[问]:可能是什么问题?先感谢您。

这是代码:

#include <iostream>

class Dimension
{
public:
    static constexpr int dim1 = 2;
    static constexpr int dim2 = 1;
};

class Vectors
{
public:
    static constexpr double vec1[2] = {4.20, 6.9};
};

int main()
{ …
Run Code Online (Sandbox Code Playgroud)

c++ class constexpr c++11

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

标签 统计

c++ ×2

c++11 ×1

c++14 ×1

clang++ ×1

class ×1

constexpr ×1

macos ×1