只是想知道是否可以在C++中声明枚举类型的函数
例如:
class myclass{
//....
enum myenum{ a, b, c, d};
myenum function();
//....
};
myenum function()
{
//....
}
Run Code Online (Sandbox Code Playgroud) 标题是自我解释的.出于某种原因,在int main()的循环中,如果用户想要输入另一本书,循环将跳过第一个函数的输入并直接询问作者的姓名.例如:
标题:指环王
作者:JRR Tolkien
版权:1954
输入以空格分隔的ISBN编号:1 2 3 x
签出?(Y或N):Y
你完成了吗?(Y或N):N //这会开始循环,Y会发出一个休息时间
标题://这一行和下一行之间实际上没有空格,应该有
作者://它跳到这一行,不允许用户输入标题,如果用户继续输入信息,这个循环继续 - 总是跳过标题的输入
码:
#include "std_lib_facilities.h"
class Book{
public:
vector<Book> books; // stores book information
Book() {}; // constructor
string what_title();
string what_author();
int what_copyright();
void store_ISBN();
void is_checkout();
private:
char check;
int ISBNfirst, ISBNsecond, ISBNthird;
char ISBNlast;
string title;
string author;
int copyright;
};
string Book::what_title()
{
cout << "Title: ";
getline(cin,title);
cout << endl;
return title;
}
string Book::what_author()
{
cout << "Author: "; …
Run Code Online (Sandbox Code Playgroud) 程序使用getline获取一个字符串,然后将该字符串传递给一个函数,在该函数中将字符串存储到由空格分隔的子字符串中.我只是通过循环读取字符来做到这一点.
但是,现在我正在尝试传递第二个字符串参数,如果循环遇到第二个字符串参数中的字符,则将字符串分隔为子字符串.这就是我到目前为止所拥有的.
#include "std_lib_facilities.h"
vector<string> split(const string& s, const string& w) // w is second argument
{
vector<string> words;
string altered;
for(int i = 0; i < s.length(); ++i)
{
altered+=s[i];
if(i == (s.length()-1)) words.push_back(altered);
else if(s[i] == ' ')
{
words.push_back(altered);
altered = "";
}
}
return words;
}
int main()
{
vector<string> words;
cout << "Enter words.\n";
string word;
getline(cin,word);
words = split(word, "aeiou"); // this for example would make the letters a, e, i, o,
// and …
Run Code Online (Sandbox Code Playgroud) 我遇到了一个容易解决的尴尬问题,但不是我喜欢做的事情.在我的类的构造函数中,我正在初始化数据成员的数据成员.这是一些代码:
class Button {
private:
// The attributes of the button
SDL_Rect box;
// The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
// Initialize the variables
explicit Button(const int x, const int y, const int w, const int h)
: box.x(x), box.y(y), box.w(w), box.h(h), clip(&clips[CLIP_MOUSEOUT]) {}
Run Code Online (Sandbox Code Playgroud)
但是,我收到编译器错误说:
C:\Users\Alex\C++\LearnSDL\mouseEvents.cpp|56|error: expected `(' before '.' token|
Run Code Online (Sandbox Code Playgroud)
和
C:\Users\Alex\C++\LearnSDL\mouseEvents.cpp|56|error: expected `{' before '.' token|
Run Code Online (Sandbox Code Playgroud)
以这种方式初始化成员是否有问题,我是否需要切换到构造函数体中的赋值?
我正在尝试从文本文件中删除元音,但我遇到了一些麻烦.我在第6行收到编译错误说
invalid conversion from const char to char
Run Code Online (Sandbox Code Playgroud)
我很确定这与我在代码中设置文件流的方式有关.我正在使用fstream,因为它用于读写,但我没有包含任何打开模式,因为我认为我可以在没有它们的情况下写入/读取文件(我很确定你必须使用一个,我只是不确定哪一个.)另外,我不确定我设置equals运算符的方式是否合法(它是否会读取它,好像s [i]等于a或e或i或o或u).
码:
#include "std_lib_facilities.h"
void vowel_removal(string& s)
{
for(int i = 0; i < s.length(); ++i)
if(s[i] == ('a' || 'e' || 'i' || 'o' || 'u')) s[i] = " ";
}
int main()
{
cout << "Enter file name.\n";
string filename;
cin >> filename;
fstream f(filename.c_str());
string word;
while(f>>word){
vowel_removal(word);
f << word;
}
keep_window_open();
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个输入 - 十六进制,八进制和小数 - 的程序,将它们存储在整数变量中,并将它们与它们转换为十进制形式一起输出.例如:
用户输入:0x43,0123,65
方案产出:
0x43 hexadecimal converts to 67 decimal
0123 octal converts to 83 decimal
65 decimal converts to 65 decimal
Run Code Online (Sandbox Code Playgroud)
显然我需要一种解释数字的方法,但我不知道如何去做.我已经尝试了各种方法,例如将它们读入函数并将它们转换为字符串,反之亦然(请参阅此处查看代码示例),但解释数字始终需要转换为某种格式来破坏原始输入.
我唯一能想到的是重载一个>>运算符,它一次读取一个字符,如果它在输入的开头看到0x或0,那么它在将整个输入读入int之前将其存储到一个字符串中.然后程序将以某种方式在输出期间确定正确的操纵器.
不确定是否有更简单的方法,任何帮助表示赞赏.
编辑:这已经解决,但我决定发布代码,如果有人有兴趣.
#include "std_lib_facilities.h"
void number_sys(string num, string& s)
{
if(num[0] == '0' && (num[1] != 'x' && num[1] != 'X')) s = "octal";
else if(num[0] == '0' && (num[1] == 'x' || num[1] == 'X')) s = "hexadecimal";
else s = "decimal";
}
int main()
{
cout << "Input numbers …
Run Code Online (Sandbox Code Playgroud) 我将智能指针的实例存储到容器中时遇到问题.这是指针的代码.
#include "std_lib_facilities.h"
template <class T>
class counted_ptr{
private:
T* pointer;
int* count;
public:
counted_ptr(T* p = 0, int* c = new int(1)) : pointer(p), count(c) {} // default constructor
explicit counted_ptr(const counted_ptr& p) : pointer(p.pointer), count(p.count) { ++*count; } // copy constructor
~counted_ptr()
{
--*count;
if(!*count) {
delete pointer;
delete count;
}
}
counted_ptr& operator=(const counted_ptr& p) // copy assignment
{
pointer = p.pointer;
count = p.count;
++*count;
return *this;
}
T* operator->() const{ return pointer; }
T& operator*() …
Run Code Online (Sandbox Code Playgroud) 假设我想让一个类中的一个变量始终与另一个变量处于某种关系而不明确地更改"链接"变量.
例如:int foo总是比int bar小10.
这样做,如果我改变了bar,foo也会改变.有没有办法做到这一点?(整数溢出实际上是不可能的,所以不要担心它.)
示例:(显然不起作用,但理解的一般代码)
class A
{
int x;
int y = x - 10; // Whenever x is changed, y will become 10 less than x
};
Run Code Online (Sandbox Code Playgroud) 我正在尝试使add_day
功能正常工作,但我遇到了一些麻烦.请注意,我不能对struct
(它非常简单)进行任何更改,因为练习的目的是使程序与给定的内容一起工作.代码是
#include "std_lib_facilities.h"
struct Date{
int y, m, d;
Date(int y, int m, int d);
void add_day(int n);
};
void Date::add_day(int n)
{
d+=n;
}
ostream& operator<<(ostream& os, const Date& d)
{
if(d.m<1 || d.m>12 || d.d<1 || d.d>31) cout << "Invalid date: ";
return os << '(' << d.y
<< ',' << d.m
<< ',' << d.d << ')';
}
int main()
{
Date today(1978,6,25);
today.add_day(1);
cout << today << endl;
keep_window_open();
}
Run Code Online (Sandbox Code Playgroud)
我收到一个链接器错误undefined …
对于我的书类,我存储了一个ISBN号,我需要验证输入的数据,所以我决定使用枚举.(前三个输入必须是单个数字,最后一个必须是字母或数字.)但是,我想知道是否甚至可以枚举数字.我已经尝试将它们作为常规整数,带双引号的字符串样式和带单引号的char样式.
例:
class Book{
public:
enum ISBN_begin{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};
enum ISBN_last{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', a, b, c, d, e, f,
g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z};
Run Code Online (Sandbox Code Playgroud)
编译器错误表示预期标识符,这意味着它将数字视为标识符的值而不是标识符本身.那么列举数字可能吗?
我知道整数以二进制表示法存储,但我想知道这会如何影响它们的读取 - 例如:
假设
cin.unsetf(ios::dec); cin.unsetf(ios::hex); and cin.unsetf(ios::oct);
Run Code Online (Sandbox Code Playgroud)
用户输入
0x43 0123 65
Run Code Online (Sandbox Code Playgroud)
它们存储为整数.现在假设程序想要将这些值识别为hex,oct或dec并执行类似的操作.
void number_sys(int num, string& s)
{
string number;
stringstream out;
out << num;
number = out.str();
if(number[0] == '0' && (number[1] != 'x' && number[1] != 'X')) s = "octal";
else if(number[0] == '0' && (number[1] == 'x' || number[1] == 'X')) s = "hexadecimal";
else s = "decimal";
}
Run Code Online (Sandbox Code Playgroud)
该函数将所有整数读为十进制.我在字符串转换后输入一些测试代码来输出字符串,字符串是十进制形式的数字.我想知道是否有一种方法可以让整数保持基本符号.
当然你可以输入数字作为字符串并测试那种方式,但是然后存在将字符串作为int读回的问题.
例如:
string a = 0x43;
int num = atoi(a.c_str());
cout << num; // will output …
Run Code Online (Sandbox Code Playgroud) 所以我有2个类,子弹和船,它们相互依赖,因此循环包含.由于我将Ship的界面#included包含在Bullet的界面中,因此显而易见的决定是将子弹声明转发给Ship.
但是,当我第一次尝试这个时,我仍然遇到编译错误.我读了一下前向声明并意识到我正在使用Ship的一个方法构建一个Bullet,并且Bullet的默认构造函数是成员初始化的,这(并且我可能是错的)不起作用,因为前向类声明不允许Ship查看界面中的定义(即成员初始化).
所以我决定放弃成员init并在Bullet的实现文件中定义构造函数,但是我仍然遇到循环依赖的相同问题.
特别是消息是invalid use of undefined type struct Bullet
.
我可以把Bullet和Ship的接口放在同一个文件中,但这是最后的手段.对此问题的任何帮助表示赞赏.谢谢.
这是发生错误的位置:
case SDLK_UP: // Fire
{
Bullet(*this) fired_bullet; // Create bullet. Line where error occurs.
fired_bullet.Move(); // Move bullet
break;
}
Run Code Online (Sandbox Code Playgroud)
Bullet的默认构造函数接受触发项目符号的Ship的参数,该代码位于Ship方法中.