在我的第一个小项目中,我是C++的新手让我头疼.
如果我理解正确的pbolem,我需要cout区间[a,b]的总和,这应该意味着:17 + 18 + 19 + 20 + 21 ..... + 52 =?(纠正我,如果我错了!)我尝试了while,do-while并且他们都结束了无限循环所以现在我尝试for循环让我只增加a的值直到它达到52.
Run Code Online (Sandbox Code Playgroud)#include <iostream> int main(int argc, char* argv[]) { const int a = 17; const int b = 52; int summe = 0; for(summe = a; summe <=b; summe++) std::cout << "Summe: " << summe << "\n"; return 0; }
假设我有一个名为Foo的类,它定义了合适的移动构造函数/赋值运算符.给定以下伪代码:
Foo some_func(Foo var) {
<update var>
return var;
}
int main() {
Foo var;
var = some_func(var);
}
Run Code Online (Sandbox Code Playgroud)
将var传递给some_func时,C++ 11会自动使用移动语义,并再次重新分配给var,因为var的原始值会被销毁吗?我认为这将是一个安全的优化,它可以让你编写与传递引用/指针一样快的纯函数.如果做不到,为什么不呢?我知道它可以用std :: move强制,但如果是自动的话它会很酷.
#include <thread>
#include <iostream>
using namespace std;
class A
{
public:
A(){}
void m(std::string* s)
{
cout<<*s;
}
void m(std::string s)
{
cout<<s;
}
};
int main()
{
A a;
string str="hi!\n";
std::thread(&A::m,a,&str);
}
Run Code Online (Sandbox Code Playgroud)
这不编译; 它给:
error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, A&, std::string*)’
Run Code Online (Sandbox Code Playgroud)
如果我删除第二个成员,它编译!为什么?我不能在std :: thread中使用重载方法?
签名vector::push_back是:
void push_back (const value_type& val);
Run Code Online (Sandbox Code Playgroud)
这意味着它有责任不腐败val.
我的功能是:
Result User::addFriend(const User* newFriend)
{
// check that newFriend is valid and is not already a friend
if (newFriend == NULL || isFriend(newFriend)) return FAILURE;
friends_.push_back(newFriend);
friendsNum_++;
return SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
当我编译.c文件时,我得到编译错误:
invalid conversion from 'const User*' to
std::vector User*::value_type
Run Code Online (Sandbox Code Playgroud)
如果push_back承诺不改变val,为什么我们会得到这个错误?
克服错误的正确和清洁方法是什么?也许是const_cast?
谢谢!
User.h:
class User {
...
public:
vector<User*> friends_;
...
}
Run Code Online (Sandbox Code Playgroud) 最近我做了一个程序,它有一个字符数组 board[8][8][2];
它基本上是一个8X8板,可以存储'2'字母字符串.我没有提供完整的代码.
但这是问题所在.
for (j = 0; j < 8; j++) {
strcpy(board[1][j], P[j].sym);
}
cout << board[1][1] << endl;
Run Code Online (Sandbox Code Playgroud)
这里P[1].sym="P1"与P[0].sym="P0"和P[2].sym="P2"
因此P[j].sym基本上是一个两个字母的字符串,board[1][j]也应该是一个两个字母的字符串.
但输出为
cout << board[1][1] << endl;
Run Code Online (Sandbox Code Playgroud)
给出为 P1P2P3P4P5P6P7
和输出
cout << board[1][0] << endl;
Run Code Online (Sandbox Code Playgroud)
给出为 P0P1P2P3P4P5P6P7
对于
cout << board[1][5] << endl;
Run Code Online (Sandbox Code Playgroud)
P5P6P7 是输出.
为了消除任何疑问,整个board[8][8][[2]已经初始化并且所有P[j].sym已经初始化.
如果它有帮助,这里是初始化的代码P:
#include <iostream>
#include <string.h>
using namespace std;
class Game
{
public:
char board[8][8][2];
char *****possibilities;
}; …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <fstream>
using namespace std;
#define BRIGHTNESS_FILE "/sys/class/backlight/radeon_b10/brightness"
int main()
{
ifstream brightness_file("BRIGHTNESS_FILE");
int a;
brightness_file >> a;
cout << a;
}
Run Code Online (Sandbox Code Playgroud)
我已经检查了文件的路径和权限.我不知道为什么它不会从中读取.
编辑
我用BRIGHTNESS_FILE修复了整个定义事物,但它仍然无法打开.为了安全起见,我多次检查了路径.
我正在尝试将字符串流从文件传递到函数.当我调用模板函数时,我收到一个错误:没有匹配函数来调用'toFile'.我验证了生命是打开的,数据已从它传递到stringstream.
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
template <typename T1>
void toFile(string type, int NumOfElements, stringstream& ss){
T1* myArray = new T1[NumOfElements]; // declaring new array to store the elements
int value;
for(int i = 0; i < NumOfElements; i++){ // store the elements in the array
ss >> value;
myArray[i] = value;
cout << myArray[i] << " ";
}
}
int main(int argc, char *argv[])
{
ifstream ins;
ofstream outs;
string strg1;
string type; …Run Code Online (Sandbox Code Playgroud) 我有两节课:
template<typename T>
class A{
public:
T& someMethod(std::string);
}
template<typename T>
class B: public A<T>{
public:
T& someMethod(T&,T&);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是知道我不能打电话
B b;
b.someMethod("HelloWorld");
Run Code Online (Sandbox Code Playgroud)
因为我的编译器看不到someMethod(std::string).你知道为什么会这样吗?
我很好奇std :: setw和std :: setfill的真正返回类型
如我们所见,引用的返回值的数据类型为“未定义”。但是,是否可以声明没有返回类型的函数?
在这种情况下,我必须开发一种为“ cout”或“ cin”提供扩展功能的方法,并且该方法应称为
cout << foo(32, 'A', 0.00f) << "Hello world!";
Run Code Online (Sandbox Code Playgroud)
我应该如何声明该方法?
我正在 C++ 中寻找一个索引容器,它具有以下属性:
我正在考虑使用类似的地图的解决方法,但我无法弄清楚。Java 中还有一个名为TreeList的容器,它具有上述属性。
c++ ×10
c++11 ×1
const ×1
const-cast ×1
containers ×1
ifstream ×1
inheritance ×1
overloading ×1
polymorphism ×1
std ×1
stdthread ×1
strcat ×1
strcpy ×1
templates ×1
vector ×1