我正在尝试用C++编译以下代码
string initialDecision ()
{
char decisionReviewUpdate;
cout << "Welcome. Type R to review, then press enter." << endl;
cin >> decisionReviewUpdate;
// Processing code
}
int main()
{
string initialDecision;
initialDecision=initialDecision();
//ERROR OCCURS HERE
// More processing code
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这里说"出现错误"的地方,我在编译时遇到以下错误:"错误:没有匹配调用'(std :: string)()'.我该如何解决这个问题?
我的问题是如何在C++上使用按位运算符std::string.通过重载或功能无关紧要.
工作XOR/^函数的示例std::string:
std::string XOR(std::string value, std::string key)
{
std::string retval(value);
long unsigned int klen = key.length();
long unsigned int vlen = value.length();
unsigned long int k = 0;
unsigned long int v = 0;
for (; v < vlen; v++) {
retval[v] = value[v] ^ key[k];
k = (++k < klen ? k : 0);
}
return retval;
}
Run Code Online (Sandbox Code Playgroud)
我现在缺少的是NOT/~,AND/&plus OR/的替代品|.示例C++代码(最后两行想要它...... :-P):
//note: matrix_content[][] holds unsigned, *checked* integers... …Run Code Online (Sandbox Code Playgroud) 我在实施课程时遇到了麻烦.它应该能够从std :: string初始化.所以我写了一个副本(?)构造函数:
CVariable (std::string&, const int p_flags = 0);
Run Code Online (Sandbox Code Playgroud)
我正在尝试制作一个CVariable的对象:
MCXJS::CVariable s_var = (string)"good job";
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
F:\Projekty\MCXJS\src\main.cpp|8|error: conversion from 'std::string' to non-scalar type 'MCXJS::CVariable' requested|
Run Code Online (Sandbox Code Playgroud)
如何解决?
我正在寻找能够完全按照以下方式获得解决方案的解决方案:
MCXJS::CVariable s_var = (string)"good job";
Run Code Online (Sandbox Code Playgroud)
编辑:添加(几乎)完整的源代码:
cvariable.h
#ifndef CVARIABLE_H
#define CVARIABLE_H
#include <string>
#include <sstream>
namespace MCXJS
{
enum VARTYPE
{
STRING = 0,
INT = 1,
FLOAT = 2
};
class CVariable
{
public:
VARTYPE Type () {return m_type;};
std::string& Value () {return m_value;};
bool SetType (VARTYPE);
private:
const …Run Code Online (Sandbox Code Playgroud) 点击这里查看功能.如您所见,除了最后三个之外的所有内容都返回对字符串的引用.第三个最后一个重载返回一个迭代器,我猜测(我可能错了......)是因为参数中的迭代器在插入操作后可能变为无效.
最后两个功能怎么样?为什么他们没有返回对字符串的引用?这背后有什么理由吗?
可以std :: string s; 在任何情况下扔?这是否受标准规范(对C++ 03感兴趣,如果存在差异)?
首先,如果这是一个令人眼花缭乱的简单而明显的问题,我想道歉.我知道对于拥有专业知识的人来说,这是一件相当容易的事情.C++ 11允许以列表形式初始化向量:
std::vector<std::string> v = {
"this is a",
"list of strings",
"which are going",
"to be stored",
"in a vector"};
Run Code Online (Sandbox Code Playgroud)
但这在旧版本中不可用.我一直在努力想出填充字符串向量的最佳方法,到目前为止我唯一能想到的就是:
std::string s1("this is a");
std::string s2("list of strings");
std::string s3("which are going");
std::string s4("to be stored");
std::string s5("in a vector");
std::vector<std::string> v;
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
v.push_back(s4);
v.push_back(s5);
Run Code Online (Sandbox Code Playgroud)
它有效,但写作有点苦差事,我相信有更好的方法.
为何分配
std::string s="aaa"+1
Run Code Online (Sandbox Code Playgroud)
工作得很好
std::string s="aaa"+1+"bbb"
Run Code Online (Sandbox Code Playgroud)
得到错误 Error 14 error C2110: '+' : cannot add two pointers
这些工作:
struct WithString {
WithString(std::string){};
};
void takeString(std::string){}
//implicit conversions:
takeString("hello");
WithString("hello");
Run Code Online (Sandbox Code Playgroud)
但这不是:
WithString makeWithString() { return "hello";}
// error: no viable conversion from returned value of type 'const char [6]'...
Run Code Online (Sandbox Code Playgroud)
如果std::string在前两种情况下隐式转换为"hello" ,为什么不能在最后一种情况下呢?请注意,我没有将WithString构造函数指定为explicit,所以我希望这样的转换.
通过这样做,我可以让行为起作用:
struct WithString {
WithString(std::string){};
WithString(const char *){};
};
Run Code Online (Sandbox Code Playgroud)
我只是对这种奇怪感到好奇.如果我假定一个猜测,我会说这是因为前两个工作的情况下,转换之间const char *到std::string,但在错误的情况下,这反而会需要2转换链,首先从const char *到std::string,然后从std::string到WithString.也许这就是原因,但我不确定.
c++ stdstring object-construction implicit-conversion char-pointer
我正在使用Centos平台上的共享库和应用程序[clang ++,llvm3.9.0和libc ++],并且库和应用程序都会重载它们自己的operator new和operator delete.
除1例外,一切正常.在调用std :: string的copy构造函数时总是调用应用程序端的operator new:
这是senario:
std::string str1 ( "A very strange issue on CentOS using clang and libc++" ); //operator new of library side called.
std::string str2(str1); //operator new of application side called. WHY??
Run Code Online (Sandbox Code Playgroud)
对于库侧调用两种情况下的operator delete.
以下是运行以下代码时的日志:
====================================================
operator new in shared library
operator new called Application side
operator delete in shared library
operator delete in shared library
====================================================
Run Code Online (Sandbox Code Playgroud)
共享库侧操作员new和delete:
void * operator new ( size_t len ) throw ( std::bad_alloc …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <string>
using namespace std;
int main ()
{
string s;
cin >> s;
for (int i = 0; i < s.size (); i++)
{
if (s[i] == 'A')
{
s[i] = "10";
}
cout << s[i];
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
main.cpp: In function
'int main()': main.cpp:10:5: error: invalid conversion from 'const char*' to 'char' [-fpermissive] s[i]= "10";
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。谢谢。
c++ ×10
stdstring ×10
c++03 ×2
arrays ×1
c++11 ×1
char-pointer ×1
clang++ ×1
constructor ×1
libc++ ×1
new-operator ×1
no-match ×1
nothrow ×1
replace ×1
stdvector ×1
string ×1
visual-c++ ×1