Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual c++ Can';我找不到错误_Visual C++ - Fatal编程技术网

Visual c++ Can';我找不到错误

Visual c++ Can';我找不到错误,visual-c++,Visual C++,这个程序是一个噩梦,它甚至不会给我运行时的错误,视觉工作室告诉我什么,我需要一些帮助 #include <iostream> using namespace std; class Textbook { private: char *aPtr; char *tPtr; int yearPub; int numPages; char bookType; public: Textbook(char *, char *, int, int,

这个程序是一个噩梦,它甚至不会给我运行时的错误,视觉工作室告诉我什么,我需要一些帮助

#include <iostream>
using namespace std;


class Textbook
{
private:
    char *aPtr;
    char *tPtr;
    int yearPub;
    int numPages;
    char bookType;
public:
    Textbook(char *, char *, int, int, char);
    void display();
    void operator=(Textbook&);
};

 Textbook::Textbook(char*string = NULL, char*string2 = NULL, int ypub = 0, int npages = 0, char btype = 'X')
{
aPtr = new char[strlen(string) +1];
strcpy(aPtr, string);

tPtr = new char[strlen(string2) +1];
strcpy(tPtr, string2);

yearPub = ypub;
numPages = npages;
bookType = btype;
}

void Textbook::display()
{
cout << "The name of the author is: " << *aPtr << endl;
cout << "The Title of the book is: " << *tPtr << endl;
cout << "The year it was published is: " << yearPub << endl;
cout << "The number of pages is: " << numPages << endl;
cout << "The initial of the title is: " << bookType << endl;
return;
}

void Textbook::operator=(Textbook& newbook)
{
if(aPtr != NULL) //check that it exists
    delete(aPtr);// delete if neccessary
aPtr = new char[strlen(newbook.aPtr) + 1];
strcpy(aPtr, newbook.aPtr);

if(tPtr != NULL) //check that it exists
    delete(tPtr); // delete if neccessary
tPtr = new char[strlen(newbook.tPtr) + 1];
strcpy(tPtr, newbook.tPtr);

yearPub = newbook.yearPub;
numPages = newbook.numPages;
bookType = newbook.bookType;
}


void main()
{
Textbook book1("sehwag", "Programming Methods", 2009, 200, 'H');
Textbook book2("Ashwin", "Security Implementation", 2011, 437, 'P');
Textbook book3;

book1.display();
book2.display();
book3.display();

book3 = book1;
book2 = book3;

book1.display();
book2.display();
book3.display();
}
#包括
使用名称空间std;
课堂教材
{
私人:
char*aPtr;
char*tPtr;
国际年酒吧;
国际货币单位;
字符类型;
公众:
教科书(char*,char*,int,int,char);
void display();
void运算符=(教科书&);
};
教科书::教科书(char*string=NULL,char*string2=NULL,int-ypub=0,int-npages=0,char-btype='X')
{
aPtr=新字符[strlen(字符串)+1];
strcpy(aPtr,字符串);
tPtr=新字符[strlen(string2)+1];
strcpy(tPtr,string2);
yearPub=ypub;
numPages=npages;
bookType=b类型;
}
作废教科书::显示()
{
不能改变:

cout << "The name of the author is: " << *aPtr << endl;
cout << "The Title of the book is: " << *tPtr << endl;
致:

对于
tptr
string2
也是如此

需要进行此检查的原因是,两个字符串输入的默认值为NULL,因此,当您在没有参数的情况下调用构造函数时(如book3),这些字符串只是NULL指针。使用NULL指针调用strlen或strcat等函数将导致异常,如您所见


理想的是,不应该使用C++风格的C++字符串——使用C++<代码>字符串< /代码> S -这将有助于避免上述问题。 不能使用空指针执行此类操作

Textbook book3;

使程序崩溃。

编译和运行程序时会发生什么情况?它说需要中断,甚至不会列出任何错误。你是否尝试在调试器中单步执行代码?如果你为问题提供更具体的标题,你更有可能得到答案。我做了,但它仍在做同样的事情,giv请告诉我,这个错误在最后的C++ +exe:0xC000中:0x5F38 D540(MVCR100D.DLL):访问违例读取位置0x00亿。当你是单步执行时,你在哪一行,你得到了例外?我想它是这条线,因为它在要求破解后说char是unasigndAPTR =新字符[SchLLN ]。(字符串)+1];具体来说,您不能将空指针传递到
strlen
strcpy
。确切地说,我刚刚查看了参考资料。我应该详细说明一下。谢谢。
aPtr = new char[strlen(string) +1];
strcpy(aPtr, string);
if (string != NULL)
{
    aPtr = new char[strlen(string) +1];
    strcpy(aPtr, string);
}
else
{
    aPtr = new char[1];
    aPtr[0] = '\0';
}
Textbook book3;