小编Ben*_*ley的帖子

是否有任何努力为C++创建包管理器?

我最喜欢的语言之一是我最大的挫折之一就是在一个统一的开发环境下让不同的库一起工作所需的努力.我最大的愿望是能够告诉我的IDE,或者其他什么,我需要一个特定的库,它负责下载它,编译它(如果需要),安装它,然后设置包含和库路径.

我想要的是这个,但是对于C++.我更喜欢它是否适用于Visual Studio,但gcc也可以.或者,如果它是它自己的独立系统,那也没关系.但是,它确实必须在Windows中运行.

哪些有前途的项目可以解决这个问题?

c++ package-managers

46
推荐指数
5
解决办法
1万
查看次数

C++将数组指针作为函数参数传递

我正在尝试使用数组指针作为生成数组的函数的参数.

void generateArray(int *a[],  int *si){
  srand(time(0));
  for (int j=0;j<*si;j++)
       *a[j]=(0+rand()%9);
} //end generateArray;

int main() {
  const int size=5;
  int a[size];

  generateArray(&a, &size);

  return 0;
} //end main
Run Code Online (Sandbox Code Playgroud)

但是当我编译它时,会出现以下消息:

cannot convert `int (*)[5]' to `int**' for argument `1' to `void generateArray(int**, int*)'
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers function

35
推荐指数
3
解决办法
13万
查看次数

load在本地路径上工作,require则不工作

loadee.rb

puts '> This is the second file.'
Run Code Online (Sandbox Code Playgroud)

loaddemo.rb

puts 'This is the first (master) program file.'
load 'loadee.rb'
puts 'And back again to the first file.'
Run Code Online (Sandbox Code Playgroud)

当我跑"ruby loaddemo.rb",这工作正常.这两个文件都在同一个目录中,这就是我运行的目录.

但是,如果我将负载更改为require,并且有或没有扩展我得到:

<internal:lib/rubygems/custom_require>:29:in `require': no such file to load
 -- loadee.rb (LoadError)
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from loaddemo.rb:2:in `<main>'
Run Code Online (Sandbox Code Playgroud)

我的问题当然是,为什么在这种情况下不需要工作?它应该,对吗?加载并要求使用不同的路径?

Ruby版本1.9.2

ruby ruby-1.9.2

22
推荐指数
2
解决办法
2万
查看次数

可以在预期正向迭代器的地方使用输入迭代器吗?

据我所知,迭代器类别的层次结构如下:

Random access -> Bi-directional -> Forward -> Input
                                           -> Output
Run Code Online (Sandbox Code Playgroud)

正确?

我一直认为有一个规则,如果算法需要特定类型的迭代器,你可以在链上提供类别的迭代器,但不能向下.所以我正在阅读这个答案,其中ildjarn 建议使用std::ifstreamwith std::istream_iteratorstd::search在文件中查找数据.我即将评论你不能这样做,因为search期望正向迭代器,并且istream_iterator是一个输入迭代器.但只是为了确保,我试过这个:

std::istringstream iss("Elephant hats for sale.");
std::istream_iterator<char> begin(iss), end;

std::string sub("hat");
auto i = std::search(begin, end, sub.begin(), sub.end());
Run Code Online (Sandbox Code Playgroud)

我没想到它会编译,但确实如此.但是,结果似乎没用,因为如果我遵循它:

while(i != end)
{
    std::cout << *i;
    ++i;
}
Run Code Online (Sandbox Code Playgroud)

没有输出.所以,我的问题是:我的编译器错误地允许我的调用search使用istream_iterator?或者没有规则阻止这种事情?

c++ iterator stl

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

是否有必要从不同的类定义移动构造函数?

考虑以下:

struct X
{
    Y y_;

    X(const Y & y) :y_(y) {}    
    X(Y && y) :y_(std::move(y)) {}
};
Run Code Online (Sandbox Code Playgroud)

是否有必要像第二个那样定义构造函数以充分利用移动语义?或者在适当的情况下会自动处理?

c++ rvalue-reference move-semantics c++11

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

如何获得通过可变参数构造函数调用的复制构造函数?

在以下代码中,可变参数构造函数被调用两次.在适当的时候,如何才能调用复制构造函数而不是可变参数构造函数的单个参数?

#include <iostream>

struct Foo
{
    Foo(const Foo &)
    {
        std::cout << "copy constructor\n";
    }

    template<typename... Args>
    Foo(Args&&... args)
    {
        std::cout << "variadic constructor\n";
    }

    std::string message;
};

int main()
{
    Foo f1;
    Foo f2(f1); // this calls the variadic constructor, but I want the copy constructor.
}
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++11

11
推荐指数
2
解决办法
1008
查看次数

获取指向迭代器引用的STL容器的指针?

例如,以下是可能的:

std::set<int> s;  
std::set<int>::iterator it = s.begin();
Run Code Online (Sandbox Code Playgroud)

我想知道是否可能相反,比方说,

std::set<int>* pSet = it->**getContainer**();  // something like this...
Run Code Online (Sandbox Code Playgroud)

c++ stl

10
推荐指数
3
解决办法
3648
查看次数

为什么指向同一个对象的指针有不同的值?

我有这段代码:

#include <iostream>

class A
{
public:
    A() : m_i(0) { }
protected:
    int m_i;
};
class B
{
public:
    B() : m_d(0.0) { }
protected:
    double m_d;
};
class C : public A, public B
{
public:
    C() : m_c('a') { }
private:
    char m_c;
};

int main()
{
    C d;
    A *b1 = &d;
    B *b2 = &d;

    std::cout << (long)b1 << std::endl <<(long)b2<< std::endl;
}
Run Code Online (Sandbox Code Playgroud)

编译并运行时,它会产生以下输出:

140734705182320
140734705182328
Run Code Online (Sandbox Code Playgroud)

不完全清楚为什么指向同一地址(&d)的不同指针具有不同的值.

提前致谢.

c++ pointers memory-address

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

模板类中的Typedef不起作用

我有以下代码的问题:

template <typename U>
class lamePtr
{
public:
    typedef U* ptr;
};

template <typename U>
class smarterPointer
{
    public:
    void funFun()
    {
        typedef lamePtr<U> someType;
        someType::ptr query;
    }
};
Run Code Online (Sandbox Code Playgroud)

如你所见,我在lamePtr中有一个typedef.在smarterPointer类里面我有一个函数funFun().我想做的是制作另一个typedef someType.直到该行,一切正常,直到我们到someType :: ptr查询行.

我想要发生的是"查询"将成为lamePtr <U> :: ptr(一个简单的值,而不是typedef;).但是,我得到编译错误(使用gcc 4.4.3):

temp.cpp: In member function ‘void smarterPointer<U>::funFun()’:
temp.cpp:15: error: expected ‘;’ before ‘query’
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

c++ templates typedef class typename

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

Boost.Asio安装问题

我已经使用bjam install安装了boost库,但是当我编译程序时:

#include boost/asio.hpp

int main()
{
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

出现这样的错误:

/tmp/ccVR3eeF.o: In function `__static_initialization_and_destruction_0(int, int)':
sda.cpp:(.text+0x52): undefined reference to `boost::system::generic_category()'
sda.cpp:(.text+0x5e): undefined reference to `boost::system::generic_category()'
sda.cpp:(.text+0x6a): undefined reference to `boost::system::system_category()'
/tmp/ccVR3eeF.o: In function `boost::asio::error::get_system_category()':
sda.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[boost::asio::error::get_system_category()]+0x5): undefined reference to `boost::system::system_category()'
/tmp/ccVR3eeF.o: In function `boost::asio::detail::posix_tss_ptr_create(unsigned int&)':
sda.cpp:(.text._ZN5boost4asio6detail20posix_tss_ptr_createERj[boost::asio::detail::posix_tss_ptr_create(unsigned int&)]+0x19): undefined reference to `pthread_key_create'
/tmp/ccVR3eeF.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service>::context>::~posix_tss_ptr()':
sda.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceEE7contextEED2Ev[boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service>::context>::~posix_tss_ptr()]+0x15): undefined reference to `pthread_key_delete'
/tmp/ccVR3eeF.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::strand_service::strand_impl>::context>::~posix_tss_ptr()':
sda.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_14strand_service11strand_implEE7contextEED2Ev[boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::strand_service::strand_impl>::context>::~posix_tss_ptr()]+0x15): undefined reference to `pthread_key_delete'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

我需要做什么?我该如何建立这些图书馆?

c++ boost-asio

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