编译时我遇到了一些错误,我无法弄清楚为什么......我的heapsort.h应该有导出类型吗?
heapsort.c
#include <stdio.h> // standard libraries already included in "list.h"
#include <stdlib.h>
#include "heap.h"
#include "heapsort.h"
void heapSort(int* keys, int numKeys){
heapHndl H = NULL;
H = buildHeap(numKeys, keys, numKeys);
for (int i = 1; i < numKeys; i++){
keys[i] = maxValue(H);
deleteMax(H);
}
freeHeap(&H);
}
Run Code Online (Sandbox Code Playgroud)
heapsort.h:
#ifndef _HEAPSORT_H_INCLUDE_
#define _HEAPSORT_H_INCLUDE_
#include <stdio.h>
#include <stdlib.h>
void heapSort(int* keys, int numKeys);
#endif
Run Code Online (Sandbox Code Playgroud)
当我使用我的客户端程序编译时,我在编译时遇到此错误:
HeapClient.o: In function `main':
HeapClient.c:(.text.startup+0x1a3): undefined reference to `heapsort'"
Run Code Online (Sandbox Code Playgroud) 我是C++的初学者,我习惯用int main()编写代码,现在我正在使用:
int main(int argc, char **argv)
我不知道这行代码到底意味着什么.所以,我在网上查找了一些答案,我发现了这段代码:
std::cout << "Have " << argc << " arguments:" << std::endl;
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这向我展示了我的论点.
我运行应用程序,控制台显示:
有1个参数:
C:\ Users\user\Documents\C++ Projects\Test\bin\Debug\Test.exe
然后它关闭了应用程序,因为在这个循环之后我有一个基于argc值的if else语句.
因此,如果argc与3不同则运行a exit(0).
我的问题是:
为什么我的论点只是我自己的应用程序位于调试路径上?
我怎样才能获得多个论点?
让我们说我们有一些数字0 to n,我们想要争夺那些规模,s并希望看到每一种可能的组合.
所以排列的数量恰好等于s! * n!/(s!*(n-s)!).
带n = 3和的示例s = 3:
0 1 2 | 0 1 3 | 0 2 1 | 0 2 3 | 0 3 1 | 0 3 2 | 1 0 2 | 1 0 3 | 1 3 2
1 2 3 | 1 2 0 | 1 3 0 | 2 0 1 | 2 1 0 | 2 0 3 | …Run Code Online (Sandbox Code Playgroud) 我在编译时遇到上述错误,如何纠正它.请帮我解决这个问题
#include <iostream>
using namespace std;
template <typename T>
class bar
{
public:
bar(){cout << "bar" <<"\n";}
};
template <typename T, typename ctor = bar<T>>
class foo
{
T i;
public:
explicit foo(ctor& c = ctor());
private:
ctor mctor;
};
template <typename T, typename ctor>
foo<T,ctor>::foo(ctor& c):mctor(c)
{
cout << "foo" <<"\n";
}
int main()
{
foo<int> f;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译:g ++ -std = c ++ 11 ctor_call.cpp
这个应该很容易.我正在玩模板,但得到编译器错误.
#include <iostream>
template <class T1, class T2>
class Pair
{
private:
T1 a;
T2 b;
public:
T1& first();
T2& second();
Pair(const T1& aval, const T2& bval) : a(aval), b(bval) {}
};
template <class T1, class T2>
T1& Pair<T1,T2>::first()
{
return a;
}
template <class T1, class T2>
T2& Pair<T1,T2>::second()
{
return b;
}
// Explicit Specialization
template <>
class Pair<double, int>
{
private:
double a;
int b;
public:
double& first();
int& second();
Pair(const double& aval, const int& bval) : …Run Code Online (Sandbox Code Playgroud) 从下面的程序的输出都将1A其A经历时c++filt -t.我可以看到返回类型在返回时被推导为值类型而不是rvalue引用类型std::move.对于大多数返回的用例来说,这是有意义的std::move,但这是什么原因?std::move返回一个右值引用,但为什么返回类型会自动推导为值类型?
#include <iostream>
#include <memory>
#include <utility>
#include <typeinfo>
struct A
: std::unique_ptr<int>
{
auto f()
{
return A();
}
auto g()
{
return std::move(A());
}
};
int main()
{
std::cout << typeid(decltype(A().f())).name() << ' ';
std::cout << typeid(decltype(A().g())).name() << ' ';
std::cout << typeid(A).name() << '\n';
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试遵循Herb Sutter的C++指南,在这种情况下更喜欢unique_ptr原始指针和shared_ptr.赞成的一个论点std::unique_ptr是shared_ptr在某些时候需要的可兑换性.
在我来说,我有一个vector的unique_ptr,我需要传递给需要的方法vector的shared_ptr.我希望能写出类似的东西:
for (auto &uniquePtr : vectorUnique)
vectorShared.push_back(make_shared<Abstract>(move(uniquePtr));
Run Code Online (Sandbox Code Playgroud)
这为我的Xcode 7.1基于工具链配置的以下错误C++11:
错误:字段类型'Abstract'是一个抽象类.
似乎STL Abstract在我使用时试图保持一个具体的类型实例make_shared.这似乎使Sutter先生的建议在许多情况下变得不可行,所以我确信我一定做错了!我求助于写作:
for (auto &uniquePtr : vectorUnique) {
auto ptr = uniquePtr.get();
auto shared = shared_ptr<Abstract>(ptr);
vectorShared.push_back(shared);
uniquePtr.release();
}
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
如果我使用a unique_ptr<T>来构建一个pimpl类,我理解调用T析构函数的编译器生成的函数需要T是一个完整的类型.但是pimpl类的移动构造函数呢?编译器生成的版本只调用了unique_ptr移动构造函数.该功能不会破坏任何东西.此外,pimpl类生成的移动构造函数是隐式的noexcept,因此它的主体不可能抛出异常,导致它必须退出并销毁unique_ptr子对象.
看起来好像下面的代码应该编译:
#include <memory>
class Widget { // has implicit move ctor
struct Impl;
std::unique_ptr<Impl> pImpl;
};
int main()
{
Widget *pw1 = nullptr;
new Widget(std::move(*pw1)); // call move ctor. Rejected by
} // gcc, clang, and MSVC
Run Code Online (Sandbox Code Playgroud)
正如评论所说,这个代码被所有gcc,clang和MSVC拒绝.每个人抱怨对不完整类型的操作无效.这是标准要求的吗?如果是这样,那是哪一部分
请注意,此处的问题是编译,因此上述代码由于取消引用空pw1指针而未定义的运行时行为无关紧要.出于同样的原因,new在最后一个语句中调用引起的内存泄漏并不重要.
我正在构造一个以a std::vector<std::unique_ptr<A> >作为参数的对象。构造函数是这样定义的
class B {
std::vector <std::unique_ptr<A> > e_;
public:
B(std::vector <std::unique_ptr<A> > e) : e_(std::move(e)){}
};
Run Code Online (Sandbox Code Playgroud)
然后用作
std::vector <std::unique_ptr<A> > e;
B b(e);
Run Code Online (Sandbox Code Playgroud)
Xcode出现错误
error: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<A, std::__1::default_delete<A> >'
:new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
Run Code Online (Sandbox Code Playgroud)
为什么即使我正在使用,错误仍然持续存在std::move()?
编辑:如果我使用B b(std::move(e))而不是错误似乎消失B b(e)),有没有办法将move逻辑移至函数的实现?
假设我让atomic<int> i;线程 A 使用 memory_order_release 执行原子存储/交换。接下来,线程 B 使用 memory_order_release 执行原子存储。线程 C 执行原子 fetch_add(0, memory_order_acquire);
线程 C 是否从线程A 和 B或仅从线程 B获取依赖项?