Sign in to follow this  

problem about header file in c++

This topic is 4260 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
*while waiting for the error*

String(const char* str=" ");

Should be

String(const char* str=NULL);

void main(){
String str1("welcome");
}

should be

int 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;

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites

This topic is 4260 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this