我正在为学校创建各种数字自动售货机,但遇到了问题。我创建了一个Items自动售货结构。然后,我创建了一个名为的结构Machine,其中包含一个vector<Items>。我想创建一个for循环,该循环遍历vector<Item>并显示项目,但出现以下错误:
C:\Users\Nate\Desktop>g++ structversion.cpp -o structversion.exe -std=c++11
structversion.cpp: In function 'int test(Machine)':
structversion.cpp:29:20: error: 'begin' was not declared in this scope
for (Item item : machine) {
^
structversion.cpp:29:20: note: suggested alternatives:
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/string:51:0,
from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/locale_classes.h:40,
from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/ios_base.h:41,
from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ios:42,
from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ostream:38,
from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/iostream:39,
from structversion.cpp:1:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/range_access.h:87:5: note: 'std::begin'
begin(_Tp (&__arr)[_Nm])
^
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/range_access.h:87:5: note: 'std::begin'
structversion.cpp:29:20: error: 'end' was not declared in this scope
for (Item item : machine) { …Run Code Online (Sandbox Code Playgroud) 在构建期间,编译器会不断分配不兼容的类型。
错误信息:
error: assigning to 'int' from incompatible type 'QString'
typeduserproperty.cpp:115:28: note: in instantiation of member function 'core::TypedUserProperty<int>::setValue' requested here
Run Code Online (Sandbox Code Playgroud)
样例代码
/**
* @brief setValue
* set value to property
* @param val
* value to set to property
* @return
* true - successfully set value
* false - invalid value
*/
template<class T>
void TypedUserProperty<T>::setValue(QVariant val)
{
if (std::is_same<T, int>::value == true)
{
this->_value = val.toInt();
}
else if (std::is_same<T, QString>::value == true)
{
this->_value = val.toString();
} …Run Code Online (Sandbox Code Playgroud) c++ templates if-statement function-templates template-classes
我尝试了以下程序:
#include <iostream>
#include <string>
int main ()
{
std::string str;
str[0] = 'o';
str[1] = 'k';
std::cout << str.length();
std::cout << "as a whole :";
std::cout << str << std::endl;
std::cout << "character by character :";
std::cout << str[0] << str[1] << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我不能使用object变量将字符串整体打印出来,为什么返回长度,0因为显然我已经使用下标运算符添加了字符,因为这将返回char引用,所以我知道这是合法的。
另外,我没有得到任何例外。就是这样。显然,std::string班上的幕后发生了很多事情,我知道我错过了一些东西。有人可以帮我吗?
我有以下一段代码,用于将二维数组转换为一维向量。数组使用 std::vector 分配,嵌套的 for_each 循环用于将二维数组的内容传输到一维数组。
#include <iostream>
#include <algorithm>
#include <vector>
#include <stdexcept>
#define UNUSED(expr) (void)(expr)
using usll = __uint64_t;
void print1d(std::vector<double> vv);
void matToVect1d(const std::vector<std::vector<double>>& v2d, std::vector<double>& v1d);
void matToVect1dEx(const std::vector<std::vector<double>>& v2d, std::vector<double>& v1d);
int main(int argc, char* argv[])
{
UNUSED(argc);
UNUSED(argv);
std::cout << std::endl;
const usll DIM0 {10};
const usll DIM1 {8};
std::vector<std::vector<double>> data2d(DIM0, std::vector<double>(DIM1));
std::vector<double> data1d(DIM0 * DIM1);
double temp = 0.0;
for (usll i{}; i<DIM0; ++i)
{
for (usll j{}; j<DIM1; ++j)
{
data2d[i][j] = temp++; …Run Code Online (Sandbox Code Playgroud) 我的班级有这个成员:
static std::unique_ptr<std::unique_ptr<ICommand>[]> changestatecommands;
Run Code Online (Sandbox Code Playgroud)
我找不到初始化它的正确方法。我希望数组被初始化,但元素未初始化,所以我可以随时编写如下内容:
changestatecommands[i] = std::make_unique<ICommand>();
Run Code Online (Sandbox Code Playgroud)
数组是在声明时立即初始化还是在运行时稍后初始化都没有关系。最理想的是,我想知道如何做到这两点。
使用 new 关键字创建对象:
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name;
public:
Person(string name) {
setName(name);
}
string getName() {
return this->name;
}
void setName(string name) {
this->name = name;
}
};
int main() {
Person *person1 = new Person("Rajat");
Person *person2 = person1;
person2->setName("Karan");
cout << person1->getName() << endl;
cout << person2->getName() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Karan
Karan
Run Code Online (Sandbox Code Playgroud)
创建没有 new 关键字的对象:
#include <iostream>
#include <string>
using namespace std;
class Person {
private: …Run Code Online (Sandbox Code Playgroud) 正如答案所指出的,这是我犯的一个愚蠢的错误,与多态性或智能指针无关。更正后的版本在接受的答案中。
==============原始问题==================
我正在尝试使智能指针与多态一起工作。在下面的原型代码中,纯virtual函数的实现Base::print()应该在Derived对象的内存块中。DerivedWrap可以访问指向Derived对象的指针。
为什么不能DerivedWrap::print()访问函数实现?
using namespace std;
class Base
{
public:
virtual void print() = 0;
};
class Derived : public Base
{
public:
Derived(int in) : i(in) {}
void print() {
cout << "int is " << i << endl;
}
private:
int i;
};
class DerivedWrap
{
public:
DerivedWrap() : DerivedWrap(make_unique<Derived>(2)) {}
DerivedWrap(unique_ptr<Base> pBase) : _pBase(move(pBase)) {}
void print()
{
_pBase->print();
}
private:
unique_ptr<Base> …Run Code Online (Sandbox Code Playgroud) 我希望我的函数采用左值引用,而绝对不是右值或临时值或其他任何值。
这是我的功能:
template<class T>
void foo(T& value) {}
// Imagine I have a class Foo
struct Foo
{
int a;
int b;
};
Run Code Online (Sandbox Code Playgroud)
当我调用 时foo(Foo{1, 2}),首先,即使我要求左值引用,它也会编译,其次,它不起作用,因为foo存储了传递值的地址,所以我稍后阅读时会得到垃圾。
如何强制foo采用左值引用?
我试图优雅地声明一个常量std::set对象,它将是另外两个常量std::set对象的合并。
#include <set>
const std::set<int> set_one = { 1,2,3 };
const std::set<int> set_two = { 11,15 };
const std::set<int> set_all = { 1,2,3,11,15 }; // this is not very elegant, duplication
Run Code Online (Sandbox Code Playgroud)
以set_all这种方式声明对象不太优雅,因为它复制了前两行的信息。有没有办法在声明中使用set_one和set_two常量set_all?
像这样的东西:
const std::set<int> set_all = set_one + set_two; // this does not compile, of course!
Run Code Online (Sandbox Code Playgroud)
#include <set>
#define SET_ONE 1, 2, 3
#define SET_TWO 11, 15
const std::set<int> set_one = { SET_ONE …Run Code Online (Sandbox Code Playgroud) 当我在 VS 下编译这段代码时,它会抛出一个警告。
if (m_nDarksideEffectAttCnt < m_DarkTargetIndex.size())
if (m_DuelWatchUserList.size() <= iIndex)
Run Code Online (Sandbox Code Playgroud)
警告:
& warning C4018: '<=' : signed/unsigned mismatch
Run Code Online (Sandbox Code Playgroud)
任何解决方案?