[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem when I remove main after testing

Posted on 2007-10-14
19
Medium Priority
?
270 Views
Last Modified: 2008-03-10
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;
//}
0
Comment
Question by:ghost8067
  • 8
  • 8
  • 3
19 Comments
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20073769
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.

0
 
LVL 7

Expert Comment

by: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.

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).
0
 

Author Comment

by:ghost8067
ID: 20073832
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;

}




 
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ghost8067
ID: 20073855
WhenI created the class project, I did it as a console application. Is that theproblem? SHouldIhave created a class library project?  
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20073870
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.
0
 

Author Comment

by:ghost8067
ID: 20073933
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

0
 
LVL 7

Expert Comment

by:UrosVidojevic
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.

This is necessary because your test program (for example test.cpp) uses this header.
0
 

Author Comment

by:ghost8067
ID: 20073971
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
 
0
 

Author Comment

by:ghost8067
ID: 20073977
Forgot to mention, still got the not found when building testString
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20073988
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.
0
 

Author Comment

by:ghost8067
ID: 20073995
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.
0
 

Author Comment

by:ghost8067
ID: 20074014
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;

}

 
0
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 2000 total points
ID: 20074034
You have a logical error in this constructor:

      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);
      }

The error occurs when you call the constructor with only one parameter.
For example:

String("ABC")
Then the constructor assingns 0 to length which is not correct.

You don't need second parameter (length), it can be calculated from strings:

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

Author Comment

by:ghost8067
ID: 20074101
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!!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20076480
>> 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.
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20076527
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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20076544
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 ?
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20076565
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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20076585
>> 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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question