C++ 使用命名空间时出错
设计并编写名为C++ 使用命名空间时出错,c++,c++11,C++,C++11,设计并编写名为Text的类,该类管理动态分配的字符串数组。实例化后,Text对象不会收到任何内容或对不可修改字符串的引用。该字符串保存文本文件的名称,该文件包含要存储在此类的对象中的记录。如果文件不存在,文本对象假定为安全的空状态。如果文件存在,则单参数构造函数为文件中包含的记录数分配内存,并将它们复制到内存中。要查看使用ifstream对象读取文本文件的语法,请参阅OOP244说明中题为“自定义文件运算符”的章节。另见 您的设计还包括以下成员功能: 复制构造函数 复制赋值运算符 移动构造函数
Text
的类,该类管理动态分配的字符串数组。实例化后,Text
对象不会收到任何内容或对不可修改字符串的引用。该字符串保存文本文件的名称,该文件包含要存储在此类的对象中的记录。如果文件不存在,文本
对象假定为安全的空状态。如果文件存在,则单参数构造函数为文件中包含的记录数分配内存,并将它们复制到内存中。要查看使用ifstream
对象读取文本文件的语法,请参阅OOP244说明中题为“自定义文件运算符”的章节。另见
您的设计还包括以下成员功能:
- 复制构造函数
- 复制赋值运算符
- 移动构造函数
- 移动赋值运算符
- 破坏者
- 名为
的成员函数,返回文本数据的记录数size\u t size()const
命名空间w3
中定义类及其实现
这是我的密码:
//Text.h
namespace w3{
class Text{
std::string file_name;
std::string* handler;
int no_of_rec=0;
public:
Text();
Text(char*);
size_t size() const;
Text(const Text& );
Text& operator=(const Text&);
Text(Text&&);
Text&& operator=(Text &&);
~Text();
}; // class Text
} //namespace w3
//Text.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include "Text.h"
namespace w3{
Text::Text(){
file_name="";
handler = nullptr;
no_of_rec = 0;
}
Text::Text(char* fname){
file_name = fname;
if (fname[0]='\0'){
file_name=" ";
std::cout << "can not find file name !!" <<std::endl;
}else{
std::fstream f(file_name);
std::string line;
if (f.is_open()){
while (std::getline(f,line,'\n')){
no_of_rec++;
}
}else{
std::cout << "file not open !!" <<std::endl;
}
//std::cout << "number of records" << size() << std::endl;
std::string* handle = new std::string[size()];
f.clear();
f.seekg(0,std::ios::beg);
int counter = 0;
while (std::getline(f,line,'\n')){
if (counter != no_of_rec){
handle[counter]=line;
counter++;
}
}
//std::cout << handle [1] <<std::endl;
}
}
size_t Text::size() const{
return (size_t)no_of_rec;
}
//-------------------Special Member Functions-----------
Text::Text(const Text& src){ //copy constructor
std::string file_name = src.file_name;
int no_of_rec = src.no_of_rec;
if (src.handler != nullptr){
handler = new std::string[src.size()];
handler = src.handler;
//std::cout << handler[123]<<std::endl;
}else{
handler = nullptr;
}
}
Text& Text::operator=(const Text& src){
if (this != &src){
int no_of_rec = src.no_of_rec;
std::string file_name = src.file_name;
if (src.handler != nullptr){
handler = new std::string[src.size()];
handler=src.handler;
}else{
handler = nullptr;
}
}
return *this;
}
Text::Text(Text&& src){
file_name=src.file_name;
handler = src.handler;
no_of_rec = src.no_of_rec;
src.file_name=" ";
src.handler=nullptr;
src.no_of_rec=0;
}
Text&& Text::operator=(Text&& src){
if (&src != this){
file_name=src.file_name;
handler = src.handler;
no_of_rec = src.no_of_rec;
src.file_name=" ";
src.handler=nullptr;
src.no_of_rec=0;
}
return std::move(*this);
}
Text::~Text(){
//delete [] handler;
}
}
#包括
#包括
#包括
#包括
#包括“Text.h”
命名空间w3{
Text::Text(){
文件名=”;
handler=nullptr;
_rec=0的无_;
}
Text::Text(char*fname){
文件名=fname;
如果(fname[0]='\0'){
文件名=”;
std::cout必须以“w3::Text”格式而不是“Text”访问.cpp文件中的文本类
例如:w3::Text::Text(consttext&src){//copy构造函数
istead:Text::Text(const Text&src){//copy constructor实际完整的错误消息是什么?您不应该#包括cpp文件。
#include <iostream>
#include <iomanip>
#include <utility>
#include <ctime>
// #include "Text.h"
#include "Text.cpp"
#define TIME(start, end) double((end) - (start)) / CLOCKS_PER_SEC
int main (int argc, char* argv[]) {
if (argc == 1) {
std::cerr << argv[0] << ": missing file operand\n";
return 1;
}
else if (argc != 2) {
std::cerr << argv[0] << ": too many arguments\n";
return 2;
}
std::clock_t cs, ce;
{
std::cout << std::fixed << std::setprecision(3);
cs = std::clock();
w3::Text a;
ce = std::clock();
std::cout << "Constructor " << TIME(cs, ce) << " seconds";
std::cout << " - a.size = " << a.size() << std::endl;
cs = std::clock();
w3::Text b(argv[1]);
ce = std::clock();
std::cout << "Constructor " << TIME(cs, ce) << " seconds";
std::cout << " - b.size = " << b.size() << std::endl;
cs = std::clock();
a = b;
ce = std::clock();
std::cout << "Copy Assignment " << TIME(cs, ce) << " seconds";
std::cout << " - a.size = " << a.size() << std::endl;
cs = std::clock();
a = std::move(b);
ce = std::clock();
std::cout << "Move Assignment " << TIME(cs, ce) << " seconds";
std::cout << " - a.size = " << a.size() << std::endl;
cs = std::clock();
w3::Text c = a;
ce = std::clock();
std::cout << "Copy Constructor " << TIME(cs, ce) << " seconds";
std::cout << " - c.size = " << c.size() << std::endl;
cs = std::clock();
w3::Text d = std::move(a);
ce = std::clock();
std::cout << "Move Constructor " << TIME(cs, ce) << " seconds";
std::cout << " - d.size = " << d.size() << std::endl;
cs = std::clock();
}
ce = std::clock();
std::cout << "Destructor " << TIME(cs, ce) << " seconds\n";
}