我试图创建自己的类(NodeWithMin)作为C++中的堆栈元素,并创建一个继承它的新类(StackWithMin).我想我可以创建新的堆栈类,但是有一些问题初始化新类的新实例并使用它.有没有人对它有好主意?我在一个文件中写了所有类和main.谢谢.
#include <stack>
class NodeWithMin{
public:
int value;
int min;
NodeWithMin(int v, int min){
this->value = v;
this->min = min;
}
};
template<class NodeWithMin>
class StackWithMin : stack<NodeWithMin>{
public:
typedef stack<NodeWithMin> super;
void push(int value){
int newMin = min(value, this->min());
super::push(new NodeWithMin(value, newMin));
};
int min(){
if(this->isEmpty()){
return numeric_limits<int>::max();
}else{
super::peek().min;
}
};
};
int main(int argc, const char * argv[])
{
StackWithMin<class NodeWithMin>* ss;
ss = new StackWithMin<class NodeWithMin>();
}
Run Code Online (Sandbox Code Playgroud) 我想问一下,你有什么方法可以简单地使用'std :: cout <<"Hello world";' 直接来自命令行.
就像你安装了python一样,
$python
print 'Hello world'
Hello world
Run Code Online (Sandbox Code Playgroud)
是否可以通过任何方式为C++完成这样的事情?
请原谅模糊的标题(我不知道如何解决这个问题).无论如何,在我的代码中我明确地声明了一些变量,两个是有符号/无符号的int变量,其他是有符号/无符号的char类型变量.
我的代码:
#include <iostream>
int main(void)
{
unsigned int number = UINT_MAX;
signed int number2 = INT_MAX;
unsigned char U = UCHAR_MAX;
signed char S = CHAR_MAX;
std::cout << number << std::endl;
std::cout << "The size in bytes of this variable is: " << sizeof(number) << std::endl << std::endl;
std::cout << number2 << std::endl;
std::cout << "The size in bytes of this variable is: " <<sizeof(number2) << std::endl << std::endl;
std::cout << U << std::endl;
std::cout …Run Code Online (Sandbox Code Playgroud) int x=8;
int k=~(x);
printf(%d",k)
Run Code Online (Sandbox Code Playgroud)
输出:9
对它的解释是:
8= 00000000 00000000 00000000 00001000
~8 = 11111111 11111111 11111111 11110111
Run Code Online (Sandbox Code Playgroud)
我们用整数分配它,因此最高有效位(MSB)是符号位bcz MSB是1因此它被视为-ve no.当你尝试打印它然后在打印之前编译器将采用2的补码因此它变为:2的补码是(~8)=9
2的补码11111111 11111111 11111111 11110111是00000000 00000000 00000000 00001000 +1 = 1001 = 9
所以我的问题是,如果我们这样做,k=-9如果我们打印k,它将打印-9.什么时候需要2的补充
我Game上课EnemyManager。EnemyManager处理敌人的产生及其背后的逻辑。问题是EnemyManager需要访问Game游戏类中的函数和其他对象。我可以想到两种解决方法。
Game*使用this中的参数之一传递类对象的地址EnemyManager。
声明一个指向Game对象的全局指针,并在初始化Game类时进行设置。然后将其插入enemymanager.cpp。
哪种更合适的方式做到这一点?
我只是想说我仍在学习C ++,所以我从关于结构的模块开始,虽然我不了解所有内容,但我认为我的理解是正确的。编译器不断给我的错误是:
错误:“。”之前的预期主表达式 令牌|
#include <iostream>
#include <fstream>
using namespace std;
struct catalog
{
char title[50];
char author[50];
char publisher[30];
int yearpublish;
double price;
};
int main()
{
catalog book;
char again;
fstream fbook;
fbook.open("book.dat", ios::out | ios::binary);
do
{
//Read data about the book
cout << "Enter the following data about a book:\n";
cout << "Title:";
cin.getline (book.title,50);
cout << "Author:";
cin.getline (book.author,50);
cout << "Publisher name:";
cin.getline (book.publisher,30);
cout << "Year publish:";
cin >> book.yearpublish;
cin.ignore();
cout << …Run Code Online (Sandbox Code Playgroud) 我正在尝试这样做:
#include <iostream>
using namespace std;
class smth {
public:
void function1 () { cout<<"before main";}
void function2 () { cout<<"after main";}
};
call function1();
int main ()
{
cout<<" in main";
return 0;
}
call funtion2();
Run Code Online (Sandbox Code Playgroud)
我希望有这样的信息:"在主要之前""主要""主要之后"
我该怎么做?
这是意味着什么:在提示用户之后,"你想输入另一个名字吗?" 如果用户输入"Y",则应提示他们添加其他名称.相反,程序运行在cin << response之后开始的循环,我认为我将条件设置为仅在用户未输入'Y','y,'N,'或'n'时运行.实际上,无论用户如何回答该问题,该程序似乎都与我想要的相反.
#include <iostream>
#include <cstring>
using namespace std;
int main(){
const int MAX_NUM = 101;
char name[MAX_NUM];
char response;
char nameCorrect = 'n';
double total = 0;
do{
do{
cout << "Please enter name: ";
cin >> name;
cin.ignore(100, '\n');
cout << "It looks like you entered " << name
<< ". Is this correct? (Y/N) " << endl;
cin >> nameCorrect;
while (nameCorrect != 'y' & nameCorrect != 'Y' & nameCorrect != 'n' & nameCorrect != …Run Code Online (Sandbox Code Playgroud) 我是c +的新手,我想从第二个.cpp页面调用一个类/函数.
错误:
错误C2512:'QueryAuthServer':没有适当的默认构造函数可用
new.h
class QueryAuthServer : public CNtlSession
{
public:
void SendCharLogInRes(CNtlPacket * pPacket);
}
Run Code Online (Sandbox Code Playgroud)
new.cpp
void QueryAuthServer::SendCharLogInRes(CNtlPacket * pPacket)
{
....
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
QueryAuthServer C;
C.SendCharLogInRes(pPacket);
Run Code Online (Sandbox Code Playgroud)
错误是在main.cpp我已经使用谷歌和查看othr页面具有相同的错误,但我不明白如何解决该错误.我读过,关于"C"的东西应该丢失,但我不知道是什么......
c++ ×8
c ×1
char ×1
inheritance ×1
loops ×1
organization ×1
outputstream ×1
stack ×1
structure ×1
templates ×1
while-loop ×1