我想先制作一个没有大小(vector<int> times)的向量,然后再在类()的构造函数中定义其大小times(size)。
我可以通过使用初始化器列表来做到这一点,如下所示
class A (int size): times(size) {};
Run Code Online (Sandbox Code Playgroud)
但是我的问题是,为什么不能在类似于以下代码的类的构造函数中执行此操作?
我的意思是为什么下面的代码是错误的?
class A
{
public:
A(int size);
private:
std::vector<int> line;
};
A::A(int size)
{
line(size);// here I got the error
}
Run Code Online (Sandbox Code Playgroud)
line(size) 犯错误
我想知道是否有一种方法可以减少下面代码中重载函数(函数编辑)的数量。
class foo
{
public:
foo(int _a, char _b, float _c) : a(_a), b(_b), c(_c){};
void edit(int new_a);
void edit(char new_b);
void edit(float new_c);
void edit(int new_a, char new_b);
void edit(int new_a, float new_c);
void edit(char new_b, float new_c);
void edit(int new_a, char new_b, float new_c);
void info();
private:
int a;
char b;
float c;
};
Run Code Online (Sandbox Code Playgroud)
这是编辑功能的实现:
void foo::edit(int new_a)
{
a = new_a;
}
void foo::edit(char new_b)
{
b = new_b;
}
void foo::edit(float new_c)
{
c = new_c; …Run Code Online (Sandbox Code Playgroud) 我想写一个程序在每句话后面做一个处理。像这样:
char letter;
while(std::cin >> letter)
{
if(letter == '\n')
{
// here do the process and show the results.
}
}
Run Code Online (Sandbox Code Playgroud)
我希望当用户按下回车键(意味着句子已完成)时,我们会执行一个过程,然后在显示一些结果后,用户可以输入新的短语,但 if(letter == '\n') 行不会没有按我的预期工作。请告诉我如何做到这一点。谢谢。
我正在学习c ++继承,这里有一个问题.如果我在main.cpp文件中创建这个简单的代码,它将没有任何问题.
但是当我在头文件中分隔文件时,它将无法正常工作,它会给我一些错误.
这是名为book.h的头文件的代码
#ifndef BOOK_H
#define BOOK_H
class book
{
private:
string name;
public:
book(string n = "default") : name(n) {};
~book() {};
void printname();
};
#endif
Run Code Online (Sandbox Code Playgroud)
这里是book.cpp的代码,我在这个文件中定义了这个类的功能.
#include <iostream>
#include <Windows.h>
#include <string>
#include "book.h"
using namespace std;
void book::printname()
{
cout << name << endl;
return;
}
Run Code Online (Sandbox Code Playgroud)
最后是main.cpp文件:
#include <iostream>
#include <Windows.h>
#include <string>
#include "book.h"
using namespace std;
int main()
{
system("color 0A");
book programing("c++");
cout << "the name of the book is ";
programing.printname();
system("pause"); …Run Code Online (Sandbox Code Playgroud)