所以我有一个class内部foo命名空间,其中包含一个friend函数。现在我希望函数的定义friend位于不同的命名空间中bar,以便可以按照下面看到的方式调用它。我得到的错误是val无法访问私有成员。
问:为什么?
#include <iostream>
namespace foo
{
template<typename T>
class myclass
{
private:
T val;
public:
myclass(T v) : val(v) {}
template<class U>
friend void myfun(myclass<U>);
};
namespace bar
{
template<class U>
void myfun(myclass<U> a)
{
std::cout << a.val;
}
} //bar
} //foo
int main()
{
foo::myclass<int> a(5);
foo::bar::myfun(a);
}
Run Code Online (Sandbox Code Playgroud) 如果任何字符串包含某个单词,我需要从字符串向量中删除一些元素.
我怎样才能写出一元谓词remove_if?
这是代码示例:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool remove_if_found(string word)
{
// ???
}
int main()
{
vector<string> data {
{ "the guitar has six strings" },
{ "the violin has four strings" },
{ "the the violin is more difficult to learn" },
{ "saxophones are a family of instruments" },
{ "the drum is a set of percussions" },
{ "the trumpet is a brass" }
};
cout << …Run Code Online (Sandbox Code Playgroud) 我有一个Register将 astd::function<void(const uint8_t* data, size_t len)>作为参数的函数。我想使用对象的成员函数作为目标。
我发现这个问题的答案是用来std::bind将第一个参数(隐式this指针)绑定到实际对象指针,然后将其用作std::function参数。
然而,这在 C++11、C++14 和 C++17 中都不再起作用了?
考虑以下测试程序。
#include <iostream>
#include <cstdint>
#include <functional>
void Register(std::function<void(const uint8_t* data, size_t len)> func) {
//Dummy - directly call into function
func(nullptr, 0);
}
class TestClass {
public:
void TestRegister() {
Register(
std::bind(&TestClass::TestTarget, this, std::placeholders::_1)
);
}
void TestTarget(const uint8_t* data, size_t len) {
(void) data;
(void) len;
std::cout << "Hello there" << std::endl;
}
}; …Run Code Online (Sandbox Code Playgroud) 以下代码将无法编译:
bool ptrLess(unique_ptr<int> ptr1, unique_ptr<int> ptr2)
{
return *ptr1 < *ptr2;
}
int main()
{
unique_ptr<int> ptr1(new int(3));
unique_ptr<int> ptr2(new int(2));
unique_ptr<int> ptr3(new int(5));
list<unique_ptr<int>> list;
list.push_back(ptr1);
list.push_back(ptr2);
list.push_back(ptr3);
list.sort(ptrLess);
for (auto &element : list) {
cout << *element;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我假设这是因为unique_ptr删除了复制构造函数.我得到一个错误:
错误C2280:'std :: unique_ptr> :: unique_ptr(const std :: unique_ptr <_Ty,std :: default_delete <_Ty >>&)':尝试引用已删除的函数
有没有办法对列表进行排序unique_ptr,可能是通过使用移动构造函数来代替?
例如,我有以下内容:
#include <iostream>
int main()
{
long long a = 2346436346346346;
int b = a;
//int b = static_cast<int>(a) //same result
std::cout << a << "\n" << b;
std::cin.get();
}
Run Code Online (Sandbox Code Playgroud)
输出:
2346436346346346
1223261034
Run Code Online (Sandbox Code Playgroud)
通过什么逻辑确实b取得了这个价值?
所以,我们有这个:
template<typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
void fun(const T& val)
{
std::cout << "val >= 0";
}
int main()
{
fun(34);
}
Run Code Online (Sandbox Code Playgroud)
想象一下,我们有该函数的其他重载。我如何才能使上述函数重载仅在 的值val大于时才编译0?
在http://en.cppreference.com/w/cpp/types/is_integral 上,我看到它operator()被重载了std::is_integral,它返回了value所以我试过这个:
template<typename T, typename = std::enable_if_t<std::is_integral<T>::value() > 0>>
Run Code Online (Sandbox Code Playgroud)
当然,它看起来是错误的,而且它是错误的,因为编译器很客气地让我知道。
如何在编译时检查变量的值?
我试图以类的朋友的身份重载ostream运算符以构建电路的组件,但是它不断返回地址。
在“ Circuit_classes.h”文件的串联电路类中:
friend ostream& operator<< (ostream& os, series_circuit const& myCircuit);
Run Code Online (Sandbox Code Playgroud)
在文件“ Circuit_classes.cpp”中:
ostream& operator<<(ostream& os, series_circuit const& myCircuit){
os << "Output: " << myCircuit.frequency << endl;
return os;
}
Run Code Online (Sandbox Code Playgroud)
在frequency类头文件中定义为60的位置。
在我的主程序中,“ AC Circuits.cpp”
vector<shared_ptr<circuit>> circuit_vector;
circuit_vector.push_back(shared_ptr<circuit>(new series_circuit));
cout << circuit_vector[0] << endl;
Run Code Online (Sandbox Code Playgroud)
运行程序时在命令行中输出:
0325E180
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个连接两个字符串而不在 C 中使用strncat/的函数strcat。我有这个,但它给出了段错误。我究竟做错了什么?
char *concat(char *str1, char *str2) {
memcpy(str1 + strlen(str1) - 1, str2, strlen(str2) + 1);
return str1;
}
Run Code Online (Sandbox Code Playgroud)
这不是家庭作业。它适用于 TI-84 Plus CE 的 C 工具链,strncat在此功能中对我不起作用。str1将是一个字符串文字。str2将是可变的。
我正在尝试 make_pair使用下面示例中的unique_ptr
(该代码是Facade Design模式的C++实现)
#include <unordered_map>
#include <utility>
#include <functional>
using namespace std;
// Step 1 : Design the Interface
class IAccount {
protected:
int accountNo;
int cash;
public:
virtual void deposit(float amount);
virtual void withdraw(float amount);
virtual void transfer(float amount);
virtual int getAccountNumber() = 0;
};
// Step 2 : Implement the interface with one or more subtypes
class Current : public IAccount{
public:
Current(int amt) { cash = amt; }
int getAccountNumber() {
return accountNo;
}
}; …Run Code Online (Sandbox Code Playgroud) 考虑以下函数,如果节点没有子节点,则从二叉搜索树中删除节点:
void erase_no_children(node* todel)
{
//...
if (todel->parent->left == todel) //if todel is left child
todel->parent->left = nullptr;
if (todel->parent->right == todel) //if todel is right child
todel->parent->right = nullptr;
delete todel;
}
Run Code Online (Sandbox Code Playgroud)
因为todel->parent->left == todel这意味着,通过设置todel->parent->left到nullptr,我只是以及设置todel到nullptr.编译器根本没有抱怨.
问题:这样做安全吗?它泄漏了吗?还是未定义的行为?