Mar 13, 2012 at 5:50pm Mar 13, 2012 at 5:50pm UTC
A program can only have one main function. If you want to share a variable between functions you can pass it as an argument to the function. Another (less recommended) way is to use global variables that can be used by any function.
Mar 15, 2012 at 2:20am Mar 15, 2012 at 2:20am UTC
I have revised my code.
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#pragma once //if something is #included more than once, the extras are ignored.
#include "Window.cpp"
#include <iostream>
#include <stdlib.h>
using namespace std;
int main ()
{
int Choice = 0;
do
{
cout << " ----menu----\n" << endl;
cout << "1:---window---" << endl;
cout << "2:---tvshow---" << endl;
cout << "3:--mychoice--" << endl;
cout << "4:----quit----\n" << endl;
cout << "Please choose: " ;
cin >> Choice;
switch (Choice)
{
default :
Choice = 0; break ;
case 1:
Winmain(); break ;
case 2:
cout << "\nunavailable..\n\n" << endl; break ;
case 3:
cout << "\nunavailable..\n\n" << endl; break ;
case 4:
cout << "\nGoodbye!" << endl; break ;
}
}while (Choice !=4);
return 0;
}
Window.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#include <iostream>
#include <stdlib.h>
#include "Window.h"
#include <string>
using namespace std;
int Retrn = 0;
void Winmain()
{
int Choice = -1;
int Input = 0;
string InputString = 0;
Window Win1;
do
{
cout << "\n\n-Window CPP-\n\n" ;
switch (Choice)
{
default :
break ;
case -1:
cout << "\nHow wide is the window: " ;
cin >> Input;
Win1.getXpos(Input);
Choice = Retrn;
break ;
case -2:
cout << "\nHow tall is the window: " ;
cin >> Input;
Win1.getYpos(Input);
Choice = Retrn;
break ;
case -3:
cout << "\nWhat application is running within the window: " ;
cin >> InputString;
Win1.getWinname(InputString);
Choice = Retrn;
break ;
case -4:
cout << "\nZ is running in a X x Y window." ;
break ;
}
}while (Choice !=-4);
}
void Window::getXpos (int getX)
{
Retrn = setXpos(getX);
}
int Window::setXpos(int setX)
{
if ((setX < 1) || (setX > 1920))
{
xposition = setX;
return -2;
}
else
return -1;
}
void Window::getYpos (int getY)
{
Retrn = setYpos(getY);
}
int Window::setYpos(int setY)
{
if ((setY < 1) || (setY > 1920))
{
yposition = setY;
return -3;
}
else
return -2;
}
void Window::getWinname (string getWin)
{
setWinname(getWin);
}
int Window::setWinname(string setWin)
{
if (setWin > "" )
{
winname = setWin;
return -4;
}
else
return -3;
}
Window.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
class Window
{
//Variables
int xposition, yposition;
string winname;
//3 set member functions
int setXpos (int );
int setYpos (int );
int setWinname (string);
//toString method
//void toString ();
public :
//3 get member functions
void getXpos (int );
void getYpos (int );
void getWinname (string);
};
Debug returns this:
1>------ Build started: Project: cppProj1, Configuration: Debug Win32 ------
1> Window.cpp
1> Main.cpp
1> Generating Code...
1>Window.obj : error LNK2005: "void __cdecl Winmain(void)" (?Winmain@@YAXXZ) already defined in Main.obj
1>Window.obj : error LNK2005: "public: void __thiscall Window::getXpos(int)" (?getXpos@Window@@QAEXH@Z) already defined in Main.obj
1>Window.obj : error LNK2005: "private: int __thiscall Window::setXpos(int)" (?setXpos@Window@@AAEHH@Z) already defined in Main.obj
1>Window.obj : error LNK2005: "public: void __thiscall Window::getYpos(int)" (?getYpos@Window@@QAEXH@Z) already defined in Main.obj
1>Window.obj : error LNK2005: "private: int __thiscall Window::setYpos(int)" (?setYpos@Window@@AAEHH@Z) already defined in Main.obj
1>Window.obj : error LNK2005: "public: void __thiscall Window::getWinname(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?getWinname@Window@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Main.obj
1>Window.obj : error LNK2005: "private: int __thiscall Window::setWinname(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setWinname@Window@@AAEHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Main.obj
1>Window.obj : error LNK2005: "int Retrn" (?Retrn@@3HA) already defined in Main.obj
1>G:\Ferg\Projects\cppProj1\Debug\cppProj1.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I don't understand what could be wrong, any and all help is again greatly appreciated!
Last edited on Mar 15, 2012 at 2:22am Mar 15, 2012 at 2:22am UTC
Mar 15, 2012 at 4:56am Mar 15, 2012 at 4:56am UTC
You include *.h files, not *.cpp files.
#pragma once is a directive for *.h files.