cannot convert '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' {aka 'std::__cxx11::basic_string<char>'} to 'const char*'gcc
Run Code Online (Sandbox Code Playgroud)
真诚地,我不知道为什么以及如何纠正这个问题。
#include <stdio.h>
#include <conio.h>
#include <string>
#include <vector>
namespace anas
{
void passwordGenerator()
{
std::vector<std::string> PasswordString;
std::string ElencoAlfabeto("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
char CharAlfabeto[ElencoAlfabeto.length()];
for (int Lettere_Create = 0; Lettere_Create < 8; Lettere_Create++)
{
int NumeroCarattere = rand() % sizeof(CharAlfabeto);
CharAlfabeto [NumeroCarattere] = ElencoAlfabeto[NumeroCarattere];
PasswordString.push_back(CharAlfabeto[NumeroCarattere]);
}
for (int Lettere_Scritte = 0; Lettere_Scritte < 8; Lettere_Scritte++)
{
printf ( PasswordString.at(Lettere_Scritte) );
}
}
}
int main()
{
system("cls");
std ::printf("the program is started... …Run Code Online (Sandbox Code Playgroud) 我仍然是核心java的学习者.我想在这里理解多态概念.
我理解了压倒一切,并对重载有疑问.
为什么我们将它称为方法重载虽然我们调用不同的方法(我的意思是只有参数不同.).
我只是觉得调用编译时绑定的不同方法非常简单,这里唯一不同的是我有相同的方法名称.
Class A {
method A(int i){}
method A(int i, int B){}
}
Run Code Online (Sandbox Code Playgroud)
请分享您的意见.
问Punith
假设我想定义一组函数,每个函数有4个重载,第一个重载采用单个参数类型int32_t,第二个采用int64_t第三个 - uint32_t第四个 - uint64_t.对于每个函数,所有重载都具有相同的实现,因此我可以定义一个函数模板:
template <typename T>
void f(T t) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
然而,这与四次重载不同,因为现在我有一个可用于实例化的每个(整数)类型的单独函数f.但是,实现细节f可能不适用于其他整数类型.为了解决这个问题,我可以将函数模板包装在四个重载函数中:
template <typename T>
void f_impl(T t) {
// ...
}
void f(int32_t value) { f_impl(value); }
void f(int64_t value) { f_impl(value); }
void f(uint32_t value) { f_impl(value); }
void f(uint64_t value) { f_impl(value); }
Run Code Online (Sandbox Code Playgroud)
它工作但每个函数需要大量代码(4个函数重载+ 1个函数模板).有没有办法简化这个?
为了澄清,这是不可取的直接使用模板,因为它没有任何意义(实施的原因或其他方式)有其特比其他类型的int32_t,int64_t,uint32_t和uint64_t.
我已经尝试过使用std::enable_if了,这个例子最好地说明了它的问题:
#include <type_traits>
#include <iostream>
template <typename …Run Code Online (Sandbox Code Playgroud) ("hello").Remove('e');
Run Code Online (Sandbox Code Playgroud)
所以String.Remove有很多重载,其中之一是:String.Remove(int startIndex)
不知怎的,我写的字符'e'被转换为a,int并且调用了WRONG重载函数.这是完全出乎意料的事情.我是否只需要忍受这个或者是否有可能提交错误,以便在下一版本的(神圣).NET框架中得到纠正?
我试图让重载的postfix运算符make day = 1每当第365天第一个剪切是我创建原型,第二个片段是实际的代码定义,我似乎无法弄清楚这一点.任何帮助,将不胜感激.基本上该程序会询问用户一年中的哪一天他们想要日期.
所以这一天可能是364,也就是12月30日.
我想让++运算符把这一天带回到第1天,每当我使用它来增加当天为365的对象时.如果对象不是365我只想让它增加1天
每当我把它改为if(day == 365)它做同样的事情时,它仍然增加1,但我最终得到366,而不是1.
我很感激帮助.这是一个家庭作业,但它让我st脚.编辑:发布更多代码.再次感谢帮助人员
#include<iostream>
#include<string>
using namespace std;
class DayOfYear {
public:
DayOfYear(int d);
void print();
static string month;
DayOfYear();
DayOfYear(string m, int d);
DayOfYear operator++(int);
int day;
};
string DayOfYear::month;
DayOfYear DayOfYear::operator++(int) {
DayOfYear temp = *this;
if (day == 365) {
day = 1;
} else {
day++;
}
return temp;
}
DayOfYear::DayOfYear(string m, int d) {
month = m;
day = d;
if (m == "January" && d > 31) …Run Code Online (Sandbox Code Playgroud) 我想为我的矩阵类重载运算符+和=.我的代码是:
friend float* operator+(const Matrix& m)
{
for(int i = 0; i < (int)(m._n*m._n); i++)
_m[i] = _m[i] + m._m[i];
};
friend Matrix& operator=(const Matrix& m)
{
std::swap(_m, m._m);
return *this;
};
Run Code Online (Sandbox Code Playgroud)
与_m数据和_n方阵的大小.但是我的编译器给了我以下错误:
main.cpp:161:45: error: ‘Matrix& operator=(const Matrix&)’ must be a nonstatic member function
main.cpp: In function ‘float* operator+(const Matrix&)’:
main.cpp:192:12: error: invalid use of non-static data member ‘Matrix::_m’
main.cpp:158:13: error: from this location
main.cpp:192:12: error: invalid use of non-static data member ‘Matrix::_m’
main.cpp:158:21: error: from …Run Code Online (Sandbox Code Playgroud) 我试图重载一个使用 Guid 作为其参数的方法,另一个方法以字符串作为其参数。
// read a student object from the dictionary
static public User Retrieve(Guid ID)
{
User user;
// find the Guid in the Dictionary
user = Users[ID];
return user;
}
static public User Retrieve(string Email)
{
User user;
Guid id;
// find the Guid in the Dictionary
using (SHA1 sha1 = SHA1.Create())
{
byte[] hash = SHA1.ComputeHash(Encoding.Default.GetBytes(Email));
id = new Guid(hash);
}
user = Users[id];
return user;
}
Run Code Online (Sandbox Code Playgroud)
测试结果 结果消息:测试方法 SRS.CRUDUsers.UpdateStudent 抛出异常:System.ArgumentException:GUID 的字节数组长度必须正好为 16 个字节。
测试方法:
public void …Run Code Online (Sandbox Code Playgroud) 我在一篇文章中读到,编译器更改了函数(具有相同名称)的名称(名称修改),以避免名称冲突.
考虑以下示例.
void fun(int x)
{
cout<<x<<endl;
}
void fun(float x)
{
cout<<x<<endl;
}
int main()
{
fun(10);
fun(10.5f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面是函数重载的一个非常简单的例子.在编译代码时,编译器将更改函数名称,以便链接器可以在两个不同的调用之间进行链接.
问题:修剪需要什么?
由于参数具有不同的数据类型,因此可能足以与正确的函数链接.为什么那么,编译器执行Name Mangling?
我无法弄清楚,为什么C ++不允许根据返回类型进行重载,因为在以下情况下,三个member(getter)函数具有不同的函数签名,即使在存储指向成员函数的指针时,我们也需要不同的mem-函数指针类型如下:
for instance T = std::string
using constRefPtr = const std::string&(MyStruct::*)() const;
using constValuePtr = const std::string(MyStruct::*)() const;
using valuePtr = std::string(MyStruct::*)() const;
Run Code Online (Sandbox Code Playgroud)
我已经阅读过这篇类似的文章,建议使用const和非成本成员函数。
问题:如何在不删除const每个成员函数的本质的情况下使以下(getter)重载工作(如果可以通过标准C ++实现)?
我正在使用C ++ 17。
#include <iostream>
#include <string>
template<typename T> class MyStruct
{
T m_val;
public:
explicit MyStruct(const T& value)
: m_val(value)
{}
const T& getVal() const { return m_val; } // get val as const ref(no copy of member)
const T getVal() const { return m_val; } // get …Run Code Online (Sandbox Code Playgroud) 如果我有 2 个构造函数重载
calculations(double vector, double angle);
calculations(double horizontalVector, double verticalVector);
Run Code Online (Sandbox Code Playgroud)
我如何确保编译器专门使用我选择的重载之一(因为每个重载在幕后做不同的事情)?
c++ constructor overloading disambiguation ambiguous-grammar