c++;类函数中的字符数组输出 我是一个真正的C++初学者,我在C++的CXE输出中有一个字符数组输出问题。要求将某个UML类转换成C++,并生成主输出参数的工作输出。代码如下: #include <iostream> #include <stdlib.h> /*My class defintion book*/ class Book { protected: long int number; char author[25]; int year; bool lent; void setLent(bool x); bool getLent(); public: Book(long int n, char a[25], int j, bool x); long int getNr(); int getYear(); void print(); }; /*Method definition Book*/ Book::Book(long int n, char a[25], int j, bool x) {number=n; author=a; year=j; lent=x;} long int Book::getNr() {return number; } int Book::getYear() {return year;} void Book::setLent(bool x) {lent=x;} bool Book::getLent() {return lent;} void Book::print() { std::cout << "Book Nr: " << number << std::endl; std::cout << "Author: " << author << std::endl; std::cout << "Year: " << year << std::endl; if (lent==0) std::cout << "Lent [yes/no]: no" << std::endl; else std::cout << "Lent [yes/no]: yes" << std::endl; } /*MAIN*/ int main() { Book b1(123456, "test", 2014, false); b1.print(); system("pause"); return 0; #包括 #包括 /*我的班级定义书*/ 课堂用书 {受保护: 长整数; char作者[25]; 国际年; 布尔伦特; void setLent(bool x); bool-getLent(); 公众: 书(长整数n,字符a[25],整数j,布尔x); long int getNr(); int getYear(); 作废打印(); }; /*方法定义书*/ 书:书(长整数n,字符a[25],整数j,布尔x) {number=n; 作者=a; 年份=j; lent=x;} long int Book::getNr() {返回编号;} int Book::getYear() {返回年份;} 无效书籍::setLent(布尔x) {lent=x;} bool Book::getLent() {return lent;} 作废书籍::打印() { std::cout
您正在打印未初始化的数据 使作者成为字符串c++;类函数中的字符数组输出 我是一个真正的C++初学者,我在C++的CXE输出中有一个字符数组输出问题。要求将某个UML类转换成C++,并生成主输出参数的工作输出。代码如下: #include <iostream> #include <stdlib.h> /*My class defintion book*/ class Book { protected: long int number; char author[25]; int year; bool lent; void setLent(bool x); bool getLent(); public: Book(long int n, char a[25], int j, bool x); long int getNr(); int getYear(); void print(); }; /*Method definition Book*/ Book::Book(long int n, char a[25], int j, bool x) {number=n; author=a; year=j; lent=x;} long int Book::getNr() {return number; } int Book::getYear() {return year;} void Book::setLent(bool x) {lent=x;} bool Book::getLent() {return lent;} void Book::print() { std::cout << "Book Nr: " << number << std::endl; std::cout << "Author: " << author << std::endl; std::cout << "Year: " << year << std::endl; if (lent==0) std::cout << "Lent [yes/no]: no" << std::endl; else std::cout << "Lent [yes/no]: yes" << std::endl; } /*MAIN*/ int main() { Book b1(123456, "test", 2014, false); b1.print(); system("pause"); return 0; #包括 #包括 /*我的班级定义书*/ 课堂用书 {受保护: 长整数; char作者[25]; 国际年; 布尔伦特; void setLent(bool x); bool-getLent(); 公众: 书(长整数n,字符a[25],整数j,布尔x); long int getNr(); int getYear(); 作废打印(); }; /*方法定义书*/ 书:书(长整数n,字符a[25],整数j,布尔x) {number=n; 作者=a; 年份=j; lent=x;} long int Book::getNr() {返回编号;} int Book::getYear() {返回年份;} 无效书籍::setLent(布尔x) {lent=x;} bool Book::getLent() {return lent;} 作废书籍::打印() { std::cout,c++,arrays,char,output,cout,C++,Arrays,Char,Output,Cout,您正在打印未初始化的数据 使作者成为字符串 #include <string> class Book { protected: long int number; std::string author; int year; bool lent; 字符数组不如std::string灵活。它们只是数据块。如果要使用字符串,请使用std::string 也可以在C++构造函数中使用初始化列表,而不是java样式< /p
#include <string>
class Book
{ protected:
long int number;
std::string author;
int year;
bool lent;
字符数组不如std::string灵活。它们只是数据块。如果要使用字符串,请使用std::string
也可以在C++构造函数中使用初始化列表,而不是java样式< /p>
Book::Book(long int n, const std::string &a, int j, bool x)
: number(n),
author(a),
year(j),
lent(x)
{ }
这不起作用的原因是,您正在将指针
author
分配给另一个指针a
,该指针随后超出范围…因此,您只能将author
指向一些垃圾。如果您想继续使用字符数组,则必须复制a
指向的所有数据:
strcpy(author, a);
但由于它是C++,所以只需要使用更容易处理的字符串:
class Book {
...
std::string author;
....
};
Book::Book(long int n, const std::string& a, int j, bool x)
: author(a), ...
{ }
您的代码中有两个错误:
Book::Book(long int n, const char a[25], int j, bool x)
{
number=n;
strncpy(author, a, 25); // author = a; doesn't work! shouldn't compile either...
year=j;
lent=x;
}
首先:变量author是指向以零结尾的字符串的指针。您可以使用strcpy()复制此字符串。因此,您需要
#包含author=a;
更好的是,将参数std::string const&a
(除非您要从中移动constructBook::author
)@Ryan:我不知道从char[25]更改为std::string是否有效。如果UML说“char[25]”,这可能是个问题。但是如果不是,使用std::string当然会更好,更不容易出错!哦,这是一个数组?那它是如何编译的呢?@Barry:你最好使用strncpy(author,a,25);
-和我的g++(版本:(Debian 4.4.5-8)4.4.5)没有编译发问者的原始代码:original.cpp:25:错误:将'char*'赋值给'char[25]'时不兼容的类型。
@StefanK。是的,这是我所期望的错误(并在gcc 4.7.3上获得)。我不同意strncpy,尤其是如果你告诉它复制的字节比作者能容纳的多15个字节!修复了帖子。现在是凌晨0:35…不知道这个错误的号码是从哪里来的。-)嗯,我应该把那本book666(666,“撒旦用66666666666666666666666666666666666…哈哈哈!”,2014,错)
进入您的库?
class Book {
...
std::string author;
....
};
Book::Book(long int n, const std::string& a, int j, bool x)
: author(a), ...
{ }
Book::Book(long int n, const char a[25], int j, bool x)
{
number=n;
strncpy(author, a, 25); // author = a; doesn't work! shouldn't compile either...
year=j;
lent=x;
}