problem about header file in c++

Started by
5 comments, last by ZodiaXlll 18 years ago
After finished reading some C++ book I started to test how it work on VC++ 6.0 and in the book it seperate header into 2 parts 1. XXX.h and 2. XXX.cpp and use it on uXXX.cpp, but I don't know how to build it in VC++. First I create project of win32 console app,then I created c++ header file forstring.h and created c++source file for string.cpp and ustring.cpp,but it did't work and show an error, so I want to know if I do something wrong and how to fix it. Thank and sorry for my bad grammar - -a //string.h #ifndef STRING_H #define STRING_H class String{ private: char* pstr; int size; public: String(const char* str=" "); ~String(){ delete [] pstr}; }; #endif //string.cpp #include <iostream> using namespace std; #include <cstring> #include <cassert> #include "string.h" String::String(const char* str){ size=static_cast<int>(strlen(str)); pstr = new char[size+1]; strcpy(pstr,str); } //uString.cpp #include <iostream> using namespace std #include "string.h" void main(){ String str1("welcome"); } [Edited by - ZodiaXlll on April 16, 2006 9:40:15 AM]
Advertisement
Well there should be an '#endif' at the end of the header file. I think that's the only problem I can see right now.
It still show an error even #endif is added.
This is error messages.
c:\program files\microsoft visual studio\myprojects\board\stu.h(3) : error C2143: syntax error : missing ';' before '<class-head>'
c:\program files\microsoft visual studio\myprojects\board\stu.h(3) : fatal error C1004: unexpected end of file found
*while waiting for the error*
String(const char* str=" ");Should beString(const char* str=NULL);void main(){String str1("welcome");}should beint main(){String str1("welcome");return 0;}


" " doesn't zero out the pointer.

I don't know what the deal is with void main, maybe it's deprecated or something, but it's considered a bad programming habbit.

EDIT: You're error is in uString.cpp

using namespace std

should be

using namespace std;
Hello?
This is another one error from another work



//student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;
#include <cstring>

enum stTypes {_Students, _Bachelor};
class Students{
private:
char* pName;
int size;
unsigned idstudent;
protected:
stTypes _types;
public:
Students(const char* pN,unsigned id=0);
~Students();
};
#endif
//student.cpp
#include <iostream>
using namespace std;
#include "student.h"
#include <cstring>

Students::Students(const char* pN,unsigned id): idstudent(id), _types(_Students){
size=static_cast<int>(strlen(pN));
pName=new char[size+1];
strcpy(pName,pN);
};
Students::~Students{
delete [] pName;
};
}
//ustudent.cpp
#include <iostream>
using namespace std;
#include "student.h"

int main(){
const int size =4;
Students stu("a boy",30466);
return 0;
}

Compiling...
student.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\HEADER\student.cpp(11) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.

HEADER.exe - 1 error(s), 0 warning(s)
Maybe I'm just seeing something wrong here, but shouldn't this:

//student.cpp#include <iostream>using namespace std;#include "student.h"#include <cstring>Students::Students(const char* pN,unsigned id): idstudent(id), _types(_Students){size=static_cast<int>(strlen(pN));pName=new char[size+1];strcpy(pName,pN);};Students::~Students{delete [] pName;};}


be
//student.cpp#include <iostream>using namespace std;#include "student.h"#include <cstring>Students::Students(const char* pN,unsigned id): idstudent(id), _types(_Students){size=static_cast<int>(strlen(pN));pName=new char[size+1];strcpy(pName,pN);}Students::~Students(){delete [] pName;}

You put extra semi-colons as if they were class declarations and you forgot the brackets on "~Students"
Hello?
Thank you

This topic is closed to new replies.

Advertisement