我收到此错误消息,代码如下:
class Money {
public:
Money(float amount, int moneyType);
string asString(bool shortVersion=true);
private:
float amount;
int moneyType;
};
Run Code Online (Sandbox Code Playgroud)
首先,我认为默认参数不允许作为C++中的第一个参数,但允许使用.
我想使用实例的属性值将默认参数传递给实例方法:
class C:
def __init__(self, format):
self.format = format
def process(self, formatting=self.format):
print(formatting)
Run Code Online (Sandbox Code Playgroud)
尝试时,我收到以下错误消息:
NameError: name 'self' is not defined
Run Code Online (Sandbox Code Playgroud)
我希望该方法的行为如下:
C("abc").process() # prints "abc"
C("abc").process("xyz") # prints "xyz"
Run Code Online (Sandbox Code Playgroud)
这里有什么问题,为什么这不起作用?我怎么能做这个工作?
我在代码中看到了一个函数声明,如下所示
void error(char const *msg, bool showKind = true, bool exit);
Run Code Online (Sandbox Code Playgroud)
我首先想到这是一个错误,因为你不能在函数中间有默认参数,但是编译器接受了这个声明.谁看过这个吗?我正在使用GCC4.5.这是GCC扩展吗?
奇怪的是,如果我把它放在一个单独的文件中并尝试编译,GCC会拒绝它.我已经仔细检查了所有内容,包括使用的编译器选项.
如果在C++中定义了一个新变量,则可以在初始化表达式中使用该变量的名称,例如:
int x = sizeof(x);
Run Code Online (Sandbox Code Playgroud)
那么函数参数的默认值又如何呢?是否允许通过名称引用参数?例如:
void f(int y = sizeof(y)) {}
Run Code Online (Sandbox Code Playgroud)
该函数在 Clang 中被接受,但在 GCC 中被拒绝,并出现错误:
'y' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
演示: https: //gcc.godbolt.org/z/YsvYnhjTb
这里是哪个编译器?
c++ compiler-errors language-lawyer function-declaration default-arguments
C++ 14标准为以下内容指定了以下声明std::exchange:
template <class T, class U = T>
T std::exchange(T& obj, U&& new_value);
Run Code Online (Sandbox Code Playgroud)
我想知道为什么U违约,T因为U可以找到感谢new_value.在什么情况下,这会导致与以下结果不同的结果:
template <class T, class U>
T std::exchange(T& obj, U&& new_value);
Run Code Online (Sandbox Code Playgroud) void func ( string word = "hello", int b ) {
// some jobs
}
in another function
//calling
func ( "", 10 ) ;
Run Code Online (Sandbox Code Playgroud)
当我编译它时,编译器会发出错误;
default argument missing for parameter
Run Code Online (Sandbox Code Playgroud)
当然,如何在不改变任何内容的情况下修复它,例如不使"int b = 0"?而且,我想使用像func(10)或func("hi")这样的函数? 我的编译器不能正常工作吗?
我收到了用户报告我开发的库中的段错误的错误报告.
错误代码的最小示例是:
#include <map>
#include <string>
#include <iostream>
void f(std::map<std::string, std::string> m = {})
{
std::cout << m.size() << "\n";
for (const auto& s: m) {
std::cout << s.first << "->" << s.second <<"\n";
}
}
int main()
{
f();
}
Run Code Online (Sandbox Code Playgroud)
当使用GCC编译时(我测试了4.8.2和4.7.3),它正确地打印0为容器的大小,但是循环内部的段错误(根本不应该执行).
不过,我可以修复通过改变声明的问题:
void f(std::map<std::string, std::string> m = std::map<std::string, std::string>{})
Run Code Online (Sandbox Code Playgroud)
复制map作品:
void f(std::map<std::string, std::string> mx = {})
{
auto m = mx;
std::cout << m.size() << "\n";
for (const auto& s: m) …Run Code Online (Sandbox Code Playgroud) C ++ 20功能std::source_location用于捕获有关调用函数的上下文的信息。当我尝试将其与可变参数模板函数一起使用时,遇到一个问题:我看不到放置source_location参数的地方。
以下操作无效,因为可变参数必须在末尾:
// doesn't work
template <typename... Args>
void debug(Args&&... args,
const std::source_location& loc = std::source_location::current());
Run Code Online (Sandbox Code Playgroud)
以下内容也不起作用,因为调用者将被介于两者之间的参数所困扰:
// doesn't work either, because ...
template <typename... Args>
void debug(const std::source_location& loc = std::source_location::current(),
Args&&... args);
// the caller will get confused
debug(42); // error: cannot convert 42 to std::source_location
Run Code Online (Sandbox Code Playgroud)
可以在可变参数模板中无缝使用的评论中告诉我std::source_location,但是我很难弄清楚该如何做。如何std::source_location与可变参数模板函数一起使用?
c++ default-arguments variadic-templates c++20 std-source-location
我尝试使用默认模板参数编写此函数:
template<typename A, typename B>
void func(int i1, int i2, A a, B b = 123){
...
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我可以这样称呼它:func(1, 2, 3)编译器应该从默认值推断出类型B,int但我得到了no instance of overloaded function。
在这种情况下,C++ 构造是否不正确并且编译器无法推断出类型?
c++ templates default default-arguments template-argument-deduction