Two .cpp and a header
Mar 13, 2012 at 5:41pm UTC
First, what I have so far.
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.h"
#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:
cout << "\nunavailable..\n\n" << endl; ; 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
#include <iostream>
#include <stdlib.h>
#include "Window.h"
#include <string>
using namespace std;
void main()
{
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 = returnData
break ;
case -2:
cout << "\nHow tall is the window: " ;
cin >> Input;
Win1.getYpos(Input);
//Choice = returnData
break ;
case -3:
cout << "\nWhat application is running within the window: " ;
cin >> InputString;
Win1.getWinname(InputString);
//Choice = returnData
break ;
case -4:
cout << "\nZ is running in a X x Y window." ;
break ;
}
}while (Choice !=-4);
}
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 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
#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
int getXpos (int );
int getYpos (int );
int getWinname (string);
};
int Window::getXpos (int getX)
{
setXpos(getX);
return -2;
}
int Window::setXpos(int setX)
{
if ((setX < 1) || (setX > 1920))
xposition = setX;
else
return -1;
}
int Window::getYpos (int getY)
{
setYpos(getY);
return -3;
}
int Window::setYpos(int setY)
{
if ((setY < 1) || (setY > 1080))
yposition = setY;
else
return -2;
}
int Window::getWinname (string getWin)
{
setWinname(getWin);
return -4;
}
int Window::setWinname(string setWin)
{
if (setWin > "" )
winname = setWin;
else
return -3;
}
My problem is that I don't know how to run Window.cpp when Choice = 1 in Main.cpp. This is my idea of what should happen:
1 2 3 4 5 6 7 8 9 10 11
cout << "Please choose: " ;
cin >> Choice;
switch (Choice)
{
default :
Choice = 0; break ;
case 1:
/*Start Window.cpp*/ break ;
case 2:...
amidoinitrite? Am I completely misusing my .cpp and .h files?
Any and all help is appreciated!
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 13, 2012 at 6:40pm UTC
your Window.h are not a .h file it is .cc file.
Window.h file will look like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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
int getXpos (int );
int getYpos (int );
int getWinname (string);
};
and all other code goes to
Window.cc file
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
#include <iostream>
#include <stdlib.h>
#include <string>
#include "Window.h"
using namespace std;
int Window::getXpos (int getX)
{
setXpos(getX);
return -2;
}
int Window::setXpos(int setX)
{
if ((setX < 1) || (setX > 1920))
xposition = setX;
else
return -1;
}
int Window::getYpos (int getY)
{
setYpos(getY);
return -3;
}
int Window::setYpos(int setY)
{
if ((setY < 1) || (setY > 1080))
yposition = setY;
else
return -2;
}
int Window::getWinname (string getWin)
{
setWinname(getWin);
return -4;
}
int Window::setWinname(string setWin)
{
if (setWin > "" )
winname = setWin;
else
return -3;
}
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 UTC
Mar 15, 2012 at 4:56am UTC
You include *.h files, not *.cpp files.
#pragma once is a directive for *.h files.
Topic archived. No new replies allowed.