The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Visual C++ Woes (Solved)

clsCorwinclsCorwin Registered User regular
edited April 2007 in Help / Advice Forum
Note, right now, I'm not asking for any help with any of my functions UNLESS they are my problem.

Basically running into another Visual Studio problem. Mind you, I am now using Visual Studio 2005 (and not VC++ Express like before).

I compile, and get these:
MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

CTrees.exe : fatal error LNK1120: 1 unresolved externals

I've googled the error and tried what was suggested on the MSDN forums (changing linker properties from console to windows and vice versa, or even setting it to none and manually defining the entry point.) Still get this linker error.

So, anyone got an idea?


Heres my various .cpp and .h files:
Node.h
#pragma once

typedef char STRING[20];

struct NODE
{
	STRING data;
	NODE *left;
	NODE *right;
};
Tree.h
#include "Node.h"
#pragma once

typedef NODE * NODEPTR;

class TREE
{
	NODEPTR root;
	int count;

	void add(NODEPTR & r, STRING str);
	void destroy(NODEPTR r);

//actual traversing functions
	void preorder(NODEPTR r);
	void inorder(NODEPTR r);
	void postorder(NODEPTR r);

public:
//basic ADT functions
	TREE();
	~TREE();
	void create();
	void destroy();
	void add(STRING str);
	void remove(STRING &str);
	bool empty();
	bool full();

//search functions
	int getCount();
	void search(STRING str);

//traversing wrapper functions
	void preorder();
	void inorder();
	void postorder();
};


Tree.cpp
#define _CRT_SECURE_NO_DEPRECATE
#include <string>
#include <iostream>
#include "Tree.h"

using namespace std;

void TREE::add(NODEPTR & r, STRING str)
{
	if( r == 0 )
	{
		NODEPTR temp;
		temp = new NODE;
		strcpy(temp->data, str);
		temp->left = 0;
		temp->right = 0;
		r = temp;
	}
	else if( str < r->data )
		add(r->left, str);
	else
		add(r->right, str);
}

void TREE::destroy(NODEPTR r)
{
	if( r!= 0 )
	{
		destroy(r->left);
		destroy(r->right);
		delete r;
	}
}

void TREE::preorder(NODEPTR r)
{
	if( r != 0 )
	{
		cout << r->data << endl;
		inorder(r->left);
		inorder(r->right);		
	}
}

void TREE::inorder(NODEPTR r)
{
	if(r != 0)
	{
		inorder(r->left);
		cout << r->data << endl;
		inorder(r->right);
	}
}

void TREE::postorder(NODEPTR r)
{
	if(r != 0)
	{
		inorder(r->left);
		inorder(r->right);
		cout << r->data << endl;
	}	
}

TREE::TREE()
{
	TREE::create();
}

TREE::~TREE()
{
	TREE::destroy();
}

void TREE::create()
{
	root = 0;				//initialize the pointer to 0
	count = 0;				//set counter
}

void TREE::destroy()
{
	destroy(root);
}

void TREE::add(STRING str)
{
	add(root, str);
}

void TREE::remove(STRING &str)
{

	count--;
}

bool TREE::empty()
{
	return root == 0;			
}

bool TREE::full()
{
	bool result = false;
	NODEPTR temp;					//create a pointer
	temp = new NODE;			//point temp to a newly create block of RAM
	if( temp == 0 )				//0 = no memory left = full
	{
		result = true;
		delete temp;
	}
	return result;				
}

int TREE::getCount()
{
	return count;
}

void TREE::search(STRING str)
{

}

void TREE::preorder()
{
	preorder(root);
}

void TREE::inorder()
{
	inorder(root);
}

void TREE::postorder()
{
	postorder(root);
}

clsCorwin on

Posts

  • PhilodoxPhilodox Registered User regular
    edited April 2007
    Your program needs an entry point so that the program can start. It doesn't necessarily have to be WinMain (although that is the Windows convention) unless you're writing a Windows gui application. I didn't see a main method in any of your source file listings.

    Philodox on
    That's a Freudian mansex if I ever cocked one.
    twinsbanneroq0.jpg
  • clsCorwinclsCorwin Registered User regular
    edited April 2007
    I have a generic one that pretty much just #inlcudes Tree.h and says int main(){ return 0; } and I still get the error.

    clsCorwin on
  • ecco the dolphinecco the dolphin Registered User regular
    edited April 2007
    Have you included the cpp file that has the main() function into your project?

    Also, I believe the main() function is looked at only if you have a console application. For Windows applications, it's WinMain so make sure that your project settings are for a console application.

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
  • PhilodoxPhilodox Registered User regular
    edited April 2007
    It might be easier to recreate the project specifically as a console application. There is a way to do it through project settings but I've never been able to do it.

    Philodox on
    That's a Freudian mansex if I ever cocked one.
    twinsbanneroq0.jpg
  • clsCorwinclsCorwin Registered User regular
    edited April 2007
    the .cpp file with the main() does #include Tree.h

    I ahve set the setting to console already, and I still get this error.

    clsCorwin on
  • SenjutsuSenjutsu thot enthusiast Registered User regular
    edited April 2007
    clsCorwin wrote: »
    the .cpp file with the main() does #include Tree.h
    ...That doesn't mean VC++ knows about the file main is in, or to link it in.

    "#include" has nothing to do with linking. All the compiler does when it sees "#include 'Tree.h'" is replace that statement with the text in the file Tree.h. It doesn't do anything about linking object files together. This isn't Java or C#.

    Senjutsu on
  • PhilodoxPhilodox Registered User regular
    edited April 2007
    In a C++ project, any file in the source files section gets compiled. So long as your file with the main() is in the source section it should be picked up.

    Philodox on
    That's a Freudian mansex if I ever cocked one.
    twinsbanneroq0.jpg
  • clsCorwinclsCorwin Registered User regular
    edited April 2007
    Umm, well let me put it this way. For EVERY other multi file project I've done this semester, they've compile just fine without link errors. And the files work together just fine.

    clsCorwin on
  • ecco the dolphinecco the dolphin Registered User regular
    edited April 2007
    Maybe you could post what your Build output is when you do a "Rebuild Solution"?

    It looks something like:

    1>
    Rebuild All started: Project: Test, Configuration: Debug Win32
    1>Deleting intermediate and output files for project 'Test', configuration 'Debug|Win32'
    1>Compiling...
    1>Source1.cpp
    1>Compiling manifest to resources...
    1>Linking...
    1>Embedding manifest...
    1>Build log was saved at "file://e:\Temp\Test\Test\Debug\BuildLog.htm"
    1>Test - 0 error(s), 0 warning(s)
    ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

    Maybe there's something there that'll give a bit more information?

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
  • clsCorwinclsCorwin Registered User regular
    edited April 2007

    Rebuild All started: Project: Trees, Configuration: Debug Win32
    Deleting intermediate and output files for project 'Trees', configuration 'Debug|Win32'
    Compiling...
    Spellcheck.cpp
    Tree.cpp
    Generating Code...
    Compiling manifest to resources...
    Linking...
    MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
    C:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\Debug\Trees.exe : fatal error LNK1120: 1 unresolved externals
    Build log was saved at "file://c:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\Trees\Debug\BuildLog.htm"
    Trees - 2 error(s), 0 warning(s)
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

    clsCorwin on
  • clsCorwinclsCorwin Registered User regular
    edited April 2007
    And the build log:
    Rebuild started: Project: Trees, Configuration: Debug|Win32

    Command Lines


    Creating temporary file "c:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\Trees\Debug\RSP00000339962848.rsp" with contents
    [
    /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /MDd /Fo"Debug\\" /Fd"Debug\vc80.pdb" /W3 /c /Wp64 /ZI /TP ".\Tree.cpp"

    ".\Spellcheck.cpp"
    ]
    Creating command line "cl.exe @c:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\Trees\Debug\RSP00000339962848.rsp /nologo /errorReport:prompt"
    Creating temporary file "c:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\Trees\Debug\TMP00000439962848.tmp" with contents
    [
    1 /* CREATEPROCESS_MANIFEST_RESOURCE_ID */ 24 /* RT_MANIFEST */ ".\\Debug\\Trees.exe.embed.manifest"
    ]
    Creating command line "rc.exe /fo".\Debug\Trees.exe.embed.manifest.res" "c:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\Trees\Debug\TMP00000439962848.tmp""
    Creating temporary file "c:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\Trees\Debug\RSP00000539962848.rsp" with contents
    [
    /OUT:"C:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\Debug\Trees.exe" /INCREMENTAL /MANIFEST /MANIFESTFILE:"Debug\Trees.exe.intermediate.manifest" /DEBUG /PDB:"c:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\debug\Trees.pdb" /SUBSYSTEM:WINDOWS /MACHINE:X86 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

    ".\Debug\Spellcheck.obj"

    ".\Debug\Tree.obj"

    ".\Debug\Trees.exe.embed.manifest.res"
    ]
    Creating command line "link.exe @c:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\Trees\Debug\RSP00000539962848.rsp /NOLOGO /ERRORREPORT:PROMPT"

    Output Window


    Compiling...
    Spellcheck.cpp
    Tree.cpp
    Generating Code...
    Compiling manifest to resources...
    Linking...
    MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
    C:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\Debug\Trees.exe : fatal error LNK1120: 1 unresolved externals

    Results


    Build log was saved at "file://c:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\Trees\Debug\BuildLog.htm"
    Trees - 2 error(s), 0 warning(s)

    clsCorwin on
  • Eggplant WizardEggplant Wizard Little Rock, ARRegistered User regular
    edited April 2007
    Have you tried just creating a new Console Application project from scratch and re-importing your source files, as was previously suggested? It's been a while since I've done non-.NET windows development, but it looks to me like you're linking against a bunch of unnecessary libraries that are only needed for GUI apps. You can screw with the linker settings until you're blue in the face, or you can take a minute to recreate the project.

    Eggplant Wizard on
    Hello
  • jclastjclast Registered User regular
    edited April 2007
    It's been a while since I wrote C++ (and I used VC++ 6.0 so my comments may not be helpful), but why are you using a #pragma once in your .h file? Hell, why are you using #pragma once at all? It's nonstandard and not all compilers support it.

    Say you're including a header file, node.h.

    If you structure your .h file this way, you'll know for certain that your .h file is actually being included one time and only one time. Compilers that claim to support #pragma once have been known to screw up and cause linker problems (at least it was common in my university).
    #ifndef __node_h__
    #define __node_h__
    
    // node.h code here
    
    #endif
    

    I have no idea if it will solve your problem or not, but it's worth a shot.

    jclast on
    camo_sig2.png
  • PhilodoxPhilodox Registered User regular
    edited April 2007
    I've been in similar situations and did my best to change the project settings to console but VS buries the GUI project settings so deeply that I doubt you could get all of them unless you know exactly what you're looking for. All in all it's just easier to recreate the project.

    Philodox on
    That's a Freudian mansex if I ever cocked one.
    twinsbanneroq0.jpg
  • clsCorwinclsCorwin Registered User regular
    edited April 2007
    I have recreated the project, still same results.

    As far as the #pragma once issue, I've not been taught one way or the other as far as #ifndef, #pragma, etc. It's just there when I "Add class," it builds the .cpp and .h file and puts the #pragma in there. I've done projects with multiple files before and the #pragma once never caused any errors.

    clsCorwin on
  • ecco the dolphinecco the dolphin Registered User regular
    edited April 2007
    Eep - I've found one problem - your SUBSYSTEM is still set to "WINDOWS", not "CONSOLE" in the original build log.
    /OUT:"C:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\Debug\Trees.exe" /INCREMENTAL /MANIFEST /MANIFESTFILE:"Debug\Trees.exe.intermediate.manifest" /DEBUG /PDB:"c:\Documents and Settings\Lucas\My Documents\Visual Studio 2005\Projects\Trees\debug\Trees.pdb" /SUBSYSTEM:WINDOWS /MACHINE:X86 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
    

    See where it goes /SUBSYSTEM:WINDOWS ?

    It should be /SUBSYSTEM:CONSOLE

    Now, you said you've since recreated the project, so could you check to make sure that the SUBSYSTEM is set to CONSOLE by looking into your BuildLog.htm file again?

    If not, try recreating the project by going:

    File -> New -> Project -> Visual C++ -> Win32 -> Win32 Console Application

    This should pop up a new window, go into "Application Settings"

    Application Type: Console application
    Additional Options: Only check "Empty Project"
    Add common header files for: Don't check anything here

    Finish

    And add all your files again.

    Re: #pragma once - it's a header protection method that's specific to Visual C++. At this stage, I wouldn't worry about it since you're just learning the language, and your programmes aren't intended to be portable. Really, at this stage, I believe that you just need to keep in mind that #pragma once won't necessarily work on other compilers.

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
  • clsCorwinclsCorwin Registered User regular
    edited April 2007
    Thats how I always build my projects, Console Application/Empty Project.

    Wierd, I had changed that option before, and I still got the same error. Maybe there was something else going on before I recreated it. Hmm, whatever. Thanks.

    clsCorwin on
Sign In or Register to comment.