小编The*_*ist的帖子

g++:错误:“pow”没有依赖于模板参数的参数,因此“pow”的声明必须可用 [-fpermissive]

此代码在 Windows 上的 Visual Studio 2010 上正确编译,但我在 Linux、g++ 上收到此错误。谁能解释一下如何解决这个问题吗?

int bits;
T scale;
std::vector<U> TwoPowers;
U cropper;

template <typename T, typename U>
ConvertToBits<T,U>::ConvertToBits(int bits, T scale)
{
    this->bits = bits;
    this->scale = scale;
    for(long i = 0; i < 64; i++)
    {
        TwoPowers.push_back(static_cast<U>(pow(2.,i))); //error appears here
    }
    cropper = 0;
    for(int i = 0; i < bits; i++)
    {
        cropper += TwoPowers[i];
    }
}
Run Code Online (Sandbox Code Playgroud)

错误信息:

错误:“pow”没有依赖于模板参数的参数,因此“pow”声明必须可用 [-fpermissive]

谢谢。

c++ templates casting g++ type-conversion

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

使用cmath时禁用math.h废话

之前我遇到了一个问题,因为函数没有重载std::.诅咒仍然时不时地发生,因为我不使用using namespace std;.

删除使用命名空间std会导致程序获得废话结果

有没有办法禁用所有那些来自c的非std函数,只能在命名空间下使用c ++函数std(不必使用using namespace std;)?

换句话说:如果我使用sin()而不是std::sin()因为我不会犯这个错误,我想得到一个错误.当然,不仅是罪,而且是每一个与之发生冲突的功能math.h.

c c++ namespaces math.h cmath

4
推荐指数
2
解决办法
659
查看次数

Python:Matplotlib:matplotlib 中的垂直对齐图

我想从多个来源读取数据并将它们绘制在彼此之上。我需要绘制它们的方式是x-axis在底部有一个标签,而其他的都应该与相同的 对齐x-axis,无论有哪些点可用。

以下是问题的示例:

import matplotlib.pylab as plt
import random
import matplotlib.gridspec as gridspec

random.seed(20)

#create x-axis of my data
x1 = range(0,10) #different range than the next one
x2 = range(1,9)

#create data (just random data corresponding the x1,x2)
data1 = [random.random() for i in x1]
data2 = [random.random()*1000 for i in x2]

gs = gridspec.GridSpec(2,1)
fig = plt.figure()

#first plot
ax = fig.add_subplot(gs[0])
ax.plot(x1,data1)
ax.set_ylabel(r'Label One', size =16)
ax.get_yaxis().set_label_coords(-0.1,0.5)
plt.tick_params(
    axis='x',          # changes apply to …
Run Code Online (Sandbox Code Playgroud)

python plot matplotlib figure subplot

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

template 模板类,如果存在则调用函数

我有一个带有模板模板参数的简单函数。它的目的是采用一个STL容器,将智能ptr转换为普通ptr(这是一个C++03项目,但我也对C++11的答案感兴趣):

template <template <typename _T, typename = std::allocator<_T> > class Container>
static Container<T*> GetRawPtrContainer(const Container<SmartPtr<T> >& input_container)
{
    Container<T*> container;
    for(typename Container<SmartPtr<T> >::const_iterator it = input_container.begin();
        it != input_container.end();
        it++)
    {
        container.push_back(it->ptr);
    }
    return container;
}
Run Code Online (Sandbox Code Playgroud)

这是类的静态成员函数SmartPtr<T>

你在这里看到的,这一切所做的就是将push_back所有元素从input_container另一个元素转移到另一个元素并返回。

您可能已经注意到,如果输入是std::vector,则插入会出现性能问题,而这对于和O(1)来说没问题。所以我想做的是在循环之前调用它(如果可能的话)(在编译时决定)std::liststd::deque

container.reserve(input_container.size());
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

c++ templates stl template-templates c++11

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

这是boost :: filesystem中的错误吗?为什么boost :: filesystem :: path :: string()在Windows和Linux上没有相同的签名?

我想要的向量转换boost::filesystem::pathstd::string使用成员函数string().我写了这个,它在Windows上工作正常(MSVC 14,2015):

std::transform(
    users.begin(), users.end(), std::back_inserter(usersStrs),
    std::mem_fn(static_cast<const std::string (PathType::*)() const>(
        &PathType::string)));
Run Code Online (Sandbox Code Playgroud)

现在我转到gcc(6.3,Debian Stretch),我的代码给出了上面签名不存在的链接错误.要修复它,我不得不将代码更改为:

std::transform(
    users.begin(), users.end(), std::back_inserter(usersStrs),
    std::mem_fn(static_cast<const std::string& (PathType::*)() const>(
        &PathType::string)))
Run Code Online (Sandbox Code Playgroud)

PS:我知道lambda解决方案更容易,我现在切换到了必需的解决方案.

起初,我认为MSVC更宽容,但后来我切换回Windows并得到相反的链接错误,第一个签名是正确的.我去了源代码(1.64,path.hpp),这就是我发现的:

#   ifdef BOOST_WINDOWS_API
    const std::string string() const
    {
      std::string tmp;
      if (!m_pathname.empty())
        path_traits::convert(&*m_pathname.begin(), &*m_pathname.begin()+m_pathname.size(),
        tmp);
      return tmp;
    }
//...
#   else   // BOOST_POSIX_API
    //  string_type is std::string, so there is no conversion
    const std::string&  string() const { return m_pathname; }
//...
#   endif
Run Code Online (Sandbox Code Playgroud)

所以我看到的原因是在Windows上,因为它默认不使用UTF-8,所以有一个临时转换.但为什么不提升Windows和Linux使用相同的API?最坏的情况是,它会花费一个字符串的副本.对?

有没有替代path::string()我应该使用的跨平台API稳定性?

c++ linux boost boost-filesystem c++11

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

优化正在扼杀我在clang 6中的整数溢出检查

我有一些针对某些财务应用程序的定点实现.它基本上是一个包含在类中的整数,它基于给定N的十进制数作为小数.该类是偏执的并检查溢出,但是当我在发布模式下运行我的测试时,它们都失败了,最后我创建了这个演示问题的最小例子:

#include <iostream>
#include <sstream>

template <typename T, typename U>
typename std::enable_if<std::is_convertible<U, std::string>::value, T>::type 
FromString(U&& str)
{
    std::stringstream ss;
    ss << str;
    T ret;
    ss >> ret;
    return ret;
}

int main()
{
    int NewAccu=32;
    int N=10;

    using T = int64_t;

    T l = 10;
    T r = FromString<T>("1" + std::string(NewAccu - N, '0'));
    if (l == 0 || r == 0) {
        return 0;
    }
    T res = l * r;
    std::cout << l << std::endl;
    std::cout …
Run Code Online (Sandbox Code Playgroud)

c++ optimization fixed-point integer-overflow c++11

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

OpenSSL:使用 ECC 公钥加密对称密钥

我有一个对称密钥,我想使用 OpenSSL 使用 ECC 公钥对其进行加密。在其高级部分 EVP 中,OpenSSL提供了一种加密“信封”的解决方案,这正是我所需要的。

但是,我希望将这些分为不同的步骤,而不是像 OpenSSL 在 EVP 中提供的那样全部合而为一。我想控制我自己用 OpenSSL 加密对称密钥的位置,并使用我自己的 C++ 包装器加密消息,并将它们都放在我选择的格式中。

如何使用 OpenSSL 用公钥加密对称密钥而不用它加密消息?这是可行的吗?

我试图在提供的示例中使用零长度的纯文本,但它崩溃了。这可能吗?

如果没有,如何在没有 EVP 的情况下使用公钥加密EC_KEY

c c++ encryption cryptography public-key-encryption

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

使用OpenGL绘图而不会杀死CPU而不进行并行化

我正在为我的工作编写一个简单但有用的OpenGL程序,其中包括显示矢量字段的外观.因此程序只需从文件中获取数据并绘制箭头.我需要绘制几千个箭头.我正在使用Qt for windows和OpenGL API.

箭头单位是圆柱体和圆锥体,在函数Arrow()中组合在一起.

for(long i = 0; i < modifiedArrows.size(); i++) {
    glColor4d(modifiedArrows[i].color.redF(),modifiedArrows[i].color.greenF(),
        modifiedArrows[i].color.blueF(),modifiedArrows[i].opacity);
    openGLobj->Arrow(modifiedArrows[i].fromX,modifiedArrows[i].fromY,
        modifiedArrows[i].fromZ,modifiedArrows[i].toX,
        modifiedArrows[i].toY,modifiedArrows[i].toZ, 
        simulationSettings->vectorsThickness);
}
Run Code Online (Sandbox Code Playgroud)

现在问题是运行一个无限循环来继续绘制这会让CPU完全忙,这不太好.我尽可能地从paintGL()函数中删除所有计算,只剩下简单的计算.我用glFlush()和glFinish()结束了paintGL()函数,但我总是把主CPU充满.

如果我删除此循环,CPU不再太忙.但无论如何我必须画出数千支箭.

除了并行化之外,还有其他解决方案吗?

c++ opengl concurrency performance qt

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

C++:Linux:调用 write() 时 TCP/IP 程序崩溃

我有一个循环,不断通过 TCP/IP 将数据写入客户端。连接打开如下:

newsockfd = accept(sockfd,
            (struct sockaddr *) &cli_addr,
            &clilen);
Run Code Online (Sandbox Code Playgroud)

以下行在循环中连续执行(睡眠时间为 0.1 秒),以便将数据写入客户端:

n = write(newsockfd,data.c_str(),data.length()+1); //+1 to include NULL in null terminated string
if(n>=0)
{
    cout<<"success"<<endl;
}
else
{
    cout<<"Fail"<<endl;
    close(newsockfd);
    newsockfd = -1;
}
Run Code Online (Sandbox Code Playgroud)

如果连接因任何原因断开,我希望服务器能够读取以接收新连接。因此,如果写入失败,我会再次准备好接受第一个命令的新连接。

我的问题如下:该方法第一次成功,所以如果客户端的连接断开,write() 返回一个负数,我立即知道连接有问题,所以我关闭它并期待一个新的连接一。服务器收到新连接,但下次使用 write() 时,程序立即崩溃。

为什么会出现这种情况?请帮忙,我是 TCP/IP 方面的新手。

如果您需要更多信息,请询问。

求助者要求:

堆栈跟踪:

错误:信号 13:

    /mnt/hgfs/Dropbox/common_src/LinuxTCP/Server/ServerLinux-build-desktop-Qt_4_8_1_in_PATH__System__Release/ServerLinux[0x402155]
    /lib/x86_64-linux-gnu/libc.so.6(+0x364a0)[0x7ffc57ac04a0]
    /lib/x86_64-linux-gnu/libpthread.so.0(write+0x10)[0x7ffc5836dcb0]
    /mnt/hgfs/Dropbox/common_src/LinuxTCP/Server/ServerLinux-build-desktop-Qt_4_8_1_in_PATH__System__Release/ServerLinux[0x4023b6]
    /mnt/hgfs/Dropbox/common_src/LinuxTCP/Server/ServerLinux-build-desktop-Qt_4_8_1_in_PATH__System__Release/ServerLinux[0x401b54]
    /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7ffc57aab76d]
    /mnt/hgfs/Dropbox/common_src/LinuxTCP/Server/ServerLinux-build-desktop-Qt_4_8_1_in_PATH__System__Release/ServerLinux[0x402081]
Run Code Online (Sandbox Code Playgroud)

变量定义:它是一个类:

身体:

int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
Run Code Online (Sandbox Code Playgroud)

构造函数启动这些东西:

LinuxTCPServer::LinuxTCPServer(int port, bool nonblocking)
{
if(nonblocking)
    sockfd = socket(AF_INET, SOCK_NONBLOCK …
Run Code Online (Sandbox Code Playgroud)

c++ unix linux tcp

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

我的服务器退出代码137

我使用C++ 11,boost :: asio和HDF5编写了一个C++服务器/客户端对.服务器运行正常一段时间(2天),然后它停止了代码137.因为我执行了无限循环的服务器,它重新启动.

不幸的是,我的错误日志没有提供足够的信息来理解问题.所以我一直试图理解这段代码的含义.似乎已达成共识,这意味着它是一个错误128+9,9意味着程序被杀死了kill -9.现在我不确定为什么会发生这种情况.我需要帮助才能找到答案.

通过进一步阅读,我发现它可能已被系统杀死,因为它超过了某个允许的执行时间,因此系统将其杀死.现在这不太可能,因为我的linux服务器是由我的大学提供的,所以他们可以应用某种安全性来做到这一点.我读到了timeoutlinux中所谓的内容.我的第一个问题是:我怎么知道这是否是问题的原因?

我的第二个问题是:我应该检查什么才能理解这个问题?你会怎么做?请指教.

如果您需要任何其他信息,请询问.

谢谢.

c++ linux error-code exit-code boost-asio

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