Link to home
Start Free TrialLog in
Avatar of ghost8067
ghost8067

asked on

Problem when I remove main after testing

Hi,
Ihave the following class working for an assignment, but the instructor indicated he will create his own main to test the functionality. However,when I comment out my main,I get thye foillowing error:

Can anyone provide a clue?

Thanks!

The error:

MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\string\Debug\strings.exe : fatal error LNK1120: 1 unresolved externals

The code

#ifndef strings_H
#define strings_H
#include <iostream>
#include <cstring>

using namespace std;

class String {
public:
      String(const char* strings ="", int len = 0):length(len){
          cout<<"\nConstructor is Called, new String object is created"<<endl;
            str = new char[length+1]; //allocate memory
            strcpy(str, strings);
      }

      String(String& strings): length(strings.length){
            cout<<"\nCopy Constructor is called: A String object is copied to another String object"<<endl;
            str = new char[length+1]; //allocate memory
            strcpy(str, strings.str);
        }
      String operator=(String& strings){
          cout<<"\nAssignment Operator is called, a String object is assigned to another String object"<<endl;
            return String(strings);
      }
      ~String(){
            cout<<"\nDestructor is Called: Stirng object is Destroyed"<<endl;        
            delete [] str;
        }

private:
      int length;
      char* str;
};
#endif

//int main(){
//String str("Hello", 5);
//String str1("World", 5);
//
//String str2(str1);
//
//str = str1;
//return 0;
//}
Avatar of UrosVidojevic
UrosVidojevic
Flag of Serbia image

That's because you tried to execute that code.
Code can't be executed without main method.
Without main() method you can only compile and build your class.
But instructor will include your header in his program (with main function) and test it without problems.
That program will be in separate file.

Try to make your own test in separate file and the compile&execute it.

#include "strings.h"
#include <iostream>
using namespace std;

int main() {
      String myString("ABC");
      ....
}

Please let me know if you succeeded in that.

Also, if you want, you can separate your code in two files:

the first file is .h file which will contain only interface of your class,
and the second file is .cpp file which will contain implementation of all methods in class.

This is not necessary since class is not big, but it is frequently done.

And of course I suppose you need some method for printing the contents of your string (or better << operator).
Avatar of ghost8067
ghost8067

ASKER

I gave compiling the class a try, and that was successful. However,when I tried to build, I got the same error referring to main. Not sure if that is okay.

MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\string\Debug\strings.exe : fatal error LNK1120: 1 unresolved externals

Code for class
#ifndef strings_H
#define strings_H
#include <iostream>
#include <cstring>

using namespace std;

class String {
public:
      String(const char* strings ="", int len = 0):length(len){
          cout<<"\nConstructor is Called, new String object is created"<<endl;
            str = new char[length+1]; //allocate memory
            strcpy(str, strings);
      }

      String(String& strings): length(strings.length){
            cout<<"\nCopy Constructor is called: A String object is copied to another String object"<<endl;
            str = new char[length+1]; //allocate memory
            strcpy(str, strings.str);
        }
      String operator=(String& strings){
          cout<<"\nAssignment Operator is called, a String object is assigned to another String object"<<endl;
            return String(strings);
      }
      ~String(){
            cout<<"\nDestructor is Called: Stirng object is Destroyed"<<endl;        
            delete [] str;
        }

private:
      int length;
      char* str;
};
#endif

//int main(){
//String str("Hello", 5);
//String str1("World", 5);
//
//String str2(str1);
//
//str = str1;
//return 0;
//}



code and error for testString

error
c:\documents and settings\administrator\my documents\visual studio 2005\projects\teststring\teststring\teststring.cpp(1) : fatal error C1083: Cannot open include file: 'strings.h': No such file or directory

#include "strings.h"
#include <iostream>
using namespace std;




int main() {
      String str("ABC");
      String str1("CDE");

       String str2(str1);

       str = str1;
return 0;

}




 
WhenI created the class project, I did it as a console application. Is that theproblem? SHouldIhave created a class library project?  
It's ok to stay a console application.

Make sure that file with class String in it is named "strings.h". I suppose you gave it some other name because VS can't find it.
I gave that a try with no success. Here is some info that may hopefully help you spot my problem

My list of folders and files

visual studio 2005 / projects / strings
Contains:
strings.ncb
strings.sln
strings.suo

visual studio 2005 / projects / strings / strings
contains:
Debug folder
strings.cpp
strings.vcproj
strings.vcproj.USER-TH3.....

visual studio 2005 / projects / strings / strings / debug
contains:
Buildlog.htm
strings.obj
vc80.idb
vc80.pdb

I find nothing with a .h. Not sure ifI should

You need a .h file.
If strings.cpp is a file with String class in it (which I suppose is true), then you should rename it into strings.h.

This is necessary because your test program (for example test.cpp) uses this header.
Okay, I recreated the class andaddeda.h file with the class in it. Inow have thjefollowing structure:

My list of folders and files

visual studio 2005 / projects / strings
Contains:
strings.ncb
strings.sln
strings.suo

visual studio 2005 / projects / strings / strings
contains:
Debug folder
strings.h
strings.vcproj
strings.vcproj.USER-TH3.....

visual studio 2005 / projects / strings / strings / debug
contains:
Buildlog.htm
vc80.idb
vc80.pdb

Test string is in another project folder.

visual studio 2005 / projects / testString
 
Forgot to mention, still got the not found when building testString
That's ok, but you cannot access one project's headers from another project.

Put your test program in same project with string.h, and let me know it it works.
The same thing will be done by your instructor:
He will make his project and put your .h file and his .cpp file in it.
I went ahead and copied the strings.htothe folder ewhere I have testStrings.cpp. The file was found,but I got a runtime error. It did the constructor call, the copy, the assignment, and called the destructor once before abending with:

HEAP CORRUPTION DETECTED after normal block (#123) at 0x00315B08
crt detected that the application wroite tomemory after the heapbuffer.
I tried with my originalmain, and it ran fine. However, with the new one it did not. So, doI have a problem with the follwing? I have no way of knowing what he wil put in his main.

The old main that works:
int main(){
String str("Hello", 5);
String str1("World", 5);

String str2(str1);

str = str1;
return 0;
}


The new main that doesn't:

int main() {
      String str("ABC");
        String str1("CDE");

String str2(str1);

str = str1;
return 0;

}

 
ASKER CERTIFIED SOLUTION
Avatar of UrosVidojevic
UrosVidojevic
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanksfor the time and help. Youcorrected exactly what Ithought might be the problem. My guess is he will not pass the length. Works like a charm!!
Avatar of Infinity08
>> If strings.cpp is a file with String class in it (which I suppose is true), then you should rename it into strings.h.

NO NO NO NO NO !!!

Don't just rename a .cpp to a .h file - that is a VERY bad idea. Not only do you lose the advantage of modular compilation, you also will run into a LOT of problems when you use it in a bigger project.

You should have TWO files : strings.h and strings.cpp.

strings.cpp should contain the implementation of all the methods, and strings.h should contain the class declaration.

Something like this :

---- strings.h ----

#ifndef STRINGS_H
#define STRINGS_H

class String {
    int a_method(int a);                    // the method signatures
    void another_method(int b);
};

#endif /* STRINGS_H */

---- ----

---- strings.cpp ----

#include "strings.h"

int String::a_method(int a) {
    // the implementation of the method
}

void String::another_method(int b) {
    // the implementation of the method
}

---- ----


And then you can add another main.cpp file that looks like this :

---- main.cpp ----

#include "strings.h"

int main(void) {
    // use the String class
    return 0;
}

---- ----


When you do it this way, you ensure modularity by placing the class specific code in a separate .cpp file (strings.cpp), and only placing the API interface in the .h file (strings.h).
That way, you can include the strings.h file when you want to use your String class in another .cpp file. The .h file will describe how you can use the String class, but does not provide the implementation.


I can't stress it enough : you SHOULD change your setup to have 3 files instead of 2 like you have now.
ID: 20073800
>>>>
Also, if you want, you can separate your code in two files:

the first file is .h file which will contain only interface of your class,
and the second file is .cpp file which will contain implementation of all methods in class.

ID: 20073949
>>>>
You need a .h file.
If strings.cpp is a file with String class in it (which I suppose is true), then you should rename it into strings.h.
UrosVidojevic, I'm afraid that ghost8067 followed your advice and renamed his strings.cpp file to a strings.h file, and didn't split it up into two files.

You should not just rename a .cpp file to a .h file (which is what you suggested). Instead, you should say to split up the files in implementation and interface.

ghost8067, can you make sure that you have 3 files with the correct contents ? Even better, can you post your current code here, as you have split it up into files ?
Infinity08, I'm repeating my post once again (I hope you see now that I have already suggested separating):
Since ghost8067 didn't do it, in one of the following posts, I suggested renaming cpp file to h file since cpp files can't be included.

UrosVidojevic: ID: 20073800
>>>>
Also, if you want, you can separate your code in two files:

the first file is .h file which will contain only interface of your class,
and the second file is .cpp file which will contain implementation of all methods in class.
>> Since ghost8067 didn't do it, in one of the following posts, I suggested renaming cpp file to h file since cpp files can't be included.

And I repeat that that was a bad suggestion.

It doesn't matter what you said earlier - what matters is that the solution that ghost8067 is using right now, is not a good one, and should be fixed.