我有以下代码:
inline bool match(const std::wstring & text1, const std::wstring & text2)
{
return match(text1.c_str(), text2.c_str());
}
inline bool match(const std::wstring & text1, const wchar_t * text2)
{
return match(text1.c_str(), text2);
}
inline bool match(const wchar_t * text1, const std::wstring & text2)
{
return match(text1, text2.c_str());
}
inline bool match(const wchar_t * text1, const wchar_t * text2)
{
return !wcscmp(text1, text2);
}
Run Code Online (Sandbox Code Playgroud)
我得到:
error C2666: 'match' : 3 overloads have similar conversions
1> could be 'bool match(const wchar_t *,const std::wstring &)' …Run Code Online (Sandbox Code Playgroud) 我写了这段代码来说明问题。在代码本身下方,您可以看到控制台打印输出。
在我的程序中,Polygon 类对象将顶点坐标存储在指向每个顶点的向量列表中。Translate() 函数所要做的就是迭代列表中的每个向量并将参数向量添加到每个项目。很简单,对吧?
Vector 类有自己的重载__add__函数。
当我编写和测试代码时,我发现该列表的成员仅在我迭代时才会更改。完成后,所有坐标都会恢复为原始值。
在我发现这个问题之后,凭直觉我做了另一个函数 - Manual_Translate(),它手动计算向量分量(不调用 Vector。__add__)
class Vector():
def __init__(self, X, Y):
self.x = X
self.y = Y
def __add__(self, Other_Vector):
return Vector((self.x + Other_Vector.x),(self.y + Other_Vector.y))
class Polygon():
def __init__(self, point_list):
self.Points = []
for item in point_list:
self.Points.append (Vector(item[0], item[1]))
def Translate (self, Translation):
for point in self.Points:
point += Translation
print (point.x, point.y) #printout from the same loop
def Manual_Translate (self, Translation):
for point in self.Points:
point.x += …Run Code Online (Sandbox Code Playgroud) 在不进行强制转换的情况下将类型分配给文字原语的方法有哪些?我知道 0.0 变成双精度数,0.0f 变成浮点数。还有其他方法可以对文字进行类型转换吗?
我去年学习了 Java,我认为在编写构造函数方面没有遇到过问题。不幸的是,我对 C# 中的重载和链接如何工作,甚至它的基本概念感到非常困惑。
我见过 :base 在继承中使用,但我不确定如何使用。我见过:这在很多地方都被使用,而且它总是让我困惑为什么要使用它。
下面是一些带有 :this 的代码的示例(为了论证,没有 setter/getter 的情况下创建了公共变量)。
public class Person
{
public string firstName;
public string lastName;
public string height;
public int age;
public string colour;
public Person():this("George", "Seville", "45cm", 10, "Black")
{
// This is the default constructor, and we're defining the default
values.
}
public Person(string firstName, string lastName, string height, int age,
string colour)
{
this.firstName = firstName;
this.lastName = lastName;
this.height = height;
this.age = age;
this.colour = colour;
}
}} …Run Code Online (Sandbox Code Playgroud) 我正在尝试为一个用作依赖项的类编写单元测试AsynchronousSocketChannel:
final AsynchronousSocketChannel channel = mock(AsynchronousSocketChannel.class);
final Client client = new Client(channel);
client.read();
verify(channel).read(isA(ByteBuffer.class), eq(client), isA(CompletionHandler.class));
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
Invalid use of argument matchers!
5 matchers expected, 3 recorded:
Run Code Online (Sandbox Code Playgroud)
发生这种情况是因为AsynchronousSocketChannel.read有 4 个不同的重载版本,并且出于某种原因verify不断选择具有 5 个参数的版本,即使我传递的匹配器与read(ByteBuffer dst, A attachment, CompletionHandler<Integer,? super A> handler).
在这个答案中,建议这确实可能是实际编译器的问题,并且可以指示编译器选择正确的重载方法,例如
verify(channel).read(
ArgumentMatchers.<ByteBuffer>isA(ByteBuffer.class),
ArgumentMatchers.<Client>eq(client),
ArgumentMatchers.<CompletionHandler>isA(CompletionHandler.class)
);
Run Code Online (Sandbox Code Playgroud)
但这样做我总是遇到同样的错误。
知道是否有可能实现这项工作吗?否则我相信我可以只使用 5 个参数重载,null作为额外的 2 个参数传递,但这对我来说有点像黑客。
我希望能够从类似字典的类中解压一个对象。
当前的:
f(**m.to_dict())
首选
f(**m)
如果存在的话,这将起作用starstarprepare:
class M:
#... __getitem__, __setitem__
def __starstarprepare__(self):
md = self.to_dict()
return md
Run Code Online (Sandbox Code Playgroud) 我已经用两个成员变量 a 和 b 创建了两个类 car 对象……我想创建一个新对象,其 a 和 b 是我之前创建的对象的 a 和 b 的乘积。
#include<iostream>
using namespace std;
class car
{
private:
int a,b;
public:
car(int x,int y)
{
a=x;
b=y;
}
void showdata()
{
cout<<a<<" "<<b<<endl;
}
car add(car c) // to multiply 'a' and 'b' of both objects and assigning to a new
object
{
car temp; // new object of class car
temp.a = a*c.a;
temp.b = b*c.b;
return temp;
}
};
int main()
{
car …Run Code Online (Sandbox Code Playgroud) 我正在尝试解决涉及 SFINAE 的相对简单的移动问题。
我的目标是找到特定类型 T 的最佳排序方式。
我有 3 种情况:
1. type T supports `sort` function
2. type T supports range i.e. have begin and end functions (lets not include comparability at this point)
3. type T is not sortable (doesn't support ranges and doesn't have sort function)
Run Code Online (Sandbox Code Playgroud)
所以我编写了基本的模板重载并尝试从 SFINAE 中受益
#include <iostream>
#include <vector>
struct HaveSort { char c; };
struct HaveRange { char c; HaveSort s; };
struct HaveNone { char c; HaveRange r; };
template<typename T>
HaveSort test_sort(decltype(&T::sort), …Run Code Online (Sandbox Code Playgroud) 考虑这段代码:
template <typename T>
T abs(const T& n)
{
if (!std::is_signed<T>::value)
throw logic_error;
if (n < 0)
return -n;
return n;
}
Run Code Online (Sandbox Code Playgroud)
我想完全禁止我的函数与变量的使用unsigned,因为它没有意义,而且可能用户甚至不知道他使用了变量unsigned。例如,我可以在某种程度上避免这个问题:
template <typename T>
T abs(const T& n)
{
if constexpr(!std::is_signed<T>::value)
n += "";
if (n < 0)
return -n;
return n;
}
Run Code Online (Sandbox Code Playgroud)
但如果我调用abs(4u),编译器错误不是很明显。就像是"can't apply += const char[1] to double"。我可以让它更明显吗?或者只是进行多次重载?
我创建了一个具有一些基本属性的 Animal 类,并添加了一个无数据构造函数。我还重载了 ostream 运算符来打印属性。
动物.cpp
#include<bits/stdc++.h>
using namespace std;
class Animal {
string name;
int action;
public:
Animal() {
name = "dog";
action = 1;
}
ostream& write(ostream& os) {
os << name << "\n" << action << "\n";
return os;
}
friend ostream& operator<<(ostream& os, Animal &animal) {
return animal.write(os);
}
};
int main() {
cout << "Animal: " << Animal() << "\n";
}
Run Code Online (Sandbox Code Playgroud)
但是我在主要错误中发现二进制表达式 ostream 和 Animal 的操作数无效。如果我声明 Animal 然后调用 cout ,效果很好。但是如何让它像这样工作(同时初始化和cout)?
overloading ×10
c++ ×6
constructor ×2
oop ×2
python ×2
templates ×2
c# ×1
chaining ×1
dictionary ×1
inheritance ×1
iteration ×1
java ×1
literals ×1
mocking ×1
mockito ×1
ostream ×1
sfinae ×1
types ×1
unit-testing ×1
wchar-t ×1
wstring ×1