is it ok like that?

This is a discussion on is it ok like that? within the C++ Programming forums, part of the General Programming Boards category; Code: void copy(Items &A, Items B) { ID.A = ID.B; mainClass.A = mainClass.B; subClass.A = subClass.B; strReq.A = strReq.B; chkReq.A ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    84

    is it ok like that?

    Code:
    void copy(Items &A, Items B)
    		{
    			ID.A = ID.B;
    		    mainClass.A = mainClass.B;
    		    subClass.A = subClass.B;
    			strReq.A = strReq.B;
    			chkReq.A = chkReq.B;
    		    quality.A = quality.B;
    		    bonding.A = bonding.B;
    		    price.A = price.B;
    		    stackSize.A = stackSize.B;
    		    primDmg.A = primDmg.B;
    		    sconDmg.A = sconDmg.B;
    		    resist.A = resist.B;
    	    	bnsOpt.A = bnsOpt.B;
    	        name.A = name.B;
    	        desc.A = desc.B;
    		    attSpeed.A = attSpeed.B;
    		    defSpeed.A = defSpeed.B;}
    
    		void Buy(int jjj)
    		{
    			for (int iii=0; iii!=MAX_INV; iii++)
    				if (charInv[iii].ID==0){ copy(charInv[iii],itemsDB[jjj]); break;}
    				else if (charInv[iii].ID==itemsDB[jjj].ID)
    					if (itemsDB[jjj].amount < itemsDB[jjj].stackSize){ itemsDB[jjj].amount++; break;}
    		}
    is it ok like that?

    nvm the ".A" and ".B" i just noticed it myself, ill go and fix it (complier didn't give me an error for some reason)
    Last edited by ExDHaos; 05-23-2009 at 04:04 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If I'm not mistaken your class consists entirely of built-in types (int, double) and std::strings. Copying instances of this class should already work when you do

    Code:
    if (charInv[iii].ID==0){ charInv[iii] = itemsDB[jjj]; break;}
    (You may be trying to assign arrays, though, in your copy function which might not work right.)
    Last edited by anon; 05-23-2009 at 04:14 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by anon View Post
    If I'm not mistaken your class consists entirely of built-in types (int, double) and std::strings. Copying instances of this class should already work when you do

    Code:
    if (charInv[iii].ID==0){ charInv[iii] = itemsDB[jjj]; break;}
    (You may be trying to assign arrays, though, in your copy function which might not work right.)
    Code:
    A.primDmg = B.primDmg;
    		    A.sconDmg = B.sconDmg;
    		    A.resist = B.resist;
    	    	A.bnsOpt = B.bnsOpt;
    well those r arrays, how can assign array to array?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You'll need to assign all the array members (in a loop).

    But why not take advantage of the assignment operator that the compiler already provides?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by anon View Post
    You'll need to assign all the array members (in a loop).

    But why not take advantage of the assignment operator that the compiler already provides?
    Code:
    #include <string>
    #include <iostream>
    using std::string;
    using std::cout;
    using std::endl;
    
    #define MAX_ITEMS 100
    #define MAX_INV 30
    
    class Items
    {
    		int id;
    		int mainClass;
    		int subClass;
    		int grade;
    		int quality;
    		int degree;
    		int bonding;
    		int price;
    		int amount;
    		int strReq;
    		int chkReq;
    		int stackSize;
    		int primDmg[3];
    		int sconDmg[3];
    		int resist[3];
    		int bnsOpt[10];
    	    string name;
    	    string desc;
    		double attSpeed;
    		double defSpeed;
    
    public:
    
    	Items(){
    		id = 0; 
    		mainClass = 0;
    		grade = 0;
    		quality = 0;
    		degree = 0;
    		bonding = 0;
    		price = 0;
    		strReq;
    		chkReq;
    		attSpeed = 0;
    		defSpeed = 0;}
    
    	void ItemsInfo (int A, int B, int C, int D, int E, int F, int G, int H, int I, int J, int K,
    		int L, int M, int N, int O, int P, int Q, int R, int S, int T, int U, int V, int W, int X,
    		int Y, int Z, int AA, int AB, string AC, string AD, double AE, double AF)
    	    {
    		    id = A;
    		    mainClass = B;
    		    subClass = C;
    			strReq = D;
    			chkReq = E;
    		    quality = F;
    		    bonding = G;
    		    price = H;
    		    stackSize = I;
    		    primDmg[0] = J;  // first element directly adds to the needed stat
    		    primDmg[1] = K;
    		    primDmg[2] = L;
    		    sconDmg[0] = M;  // first element directly adds to the needed stat
    		    sconDmg[1] = N;
    		    sconDmg[2] = O;
    		    resist[0] = P;
    		    resist[1] = Q;
    	    	resist[2] = R;
    	    	bnsOpt[0] = S;       //each array here is directly added to the player stats
    	    	bnsOpt[1] = T;
    	    	bnsOpt[2] = U;
    	    	bnsOpt[3] = V;
    	    	bnsOpt[4] = W;
    	    	bnsOpt[5] = X;
    	    	bnsOpt[6] = Y;
    	    	bnsOpt[7] = Z;
    	    	bnsOpt[8] = AA;
    	    	bnsOpt[9] = AB;
    	        name = AC;
    	        desc = AD;
    		    attSpeed = AE;
    		    defSpeed = AF;}
    
    	int ID(){return id;}
    
    
    	double Quality()
    	{
    		if (quality==0) return 0.7;
    		else if (quality==1) return 1.0;
    		else if (quality==2) return 1.3;
    		else if (quality==3) return 1.7;
    		else if (quality==4) return 2.1;
    		else if (quality==5) return 2.6;
    		else if (quality==6) return 3.1;
    	    else return 1.0;}
    
    	double Grade()
    	{
    		if (grade==0) return 1.0;
    		else if (grade==1) return 1.1;
    		else if (grade==2) return 1.2;
    		else if (grade==3) return 1.4;
    		else if (grade==4) return 1.6;
    		else if (grade==5) return 2.0;
    		else if (grade==6) return 2.4;
    		else if (grade==7) return 3.2;
    	    else return 1.0;}
    
    	double Degree()
    	{
    		if (degree==0) return 1.0;
    		else if (degree==1) return 1.1;
    		else if (degree==2) return 1.2;
    		else if (degree==3) return 1.4;
    		else if (degree==4) return 1.6;
    		else if (degree==5) return 1.9;
    		else if (degree==6) return 2.2;
    	    else if (degree==7) return 2.6;
    		else if (degree==8) return 3.0;
    		else if (degree==9) return 3.5;
    		else if (degree==10) return 4.0;
    		else if (degree==11) return 4.6;
    		else if (degree==12) return 5.2;
    		else if (degree==13) return 5.9;
    		else if (degree==14) return 6.6;
    		else if (degree==15) return 7.4;
    	    else return 1.0;};
    
    		int ArmRes(){return (resist[0]*Quality())*Grade();}
    		int EleRes(){return (resist[1]*Quality())*Grade();}
    		int MagRes(){return (resist[2]*Quality())*Grade();}
    
    		int MinDmg(){return (primDmg[1]*Quality())*Grade();}
    		int MaxDmg(){return (primDmg[2]*Quality())*Grade();}
    		int Min2Dmg(){return (primDmg[1]*Quality())*Grade();}
    		int Max2Dmg(){return (primDmg[2]*Quality())*Grade();}
    
    		/*void copy(Items &A, Items B)
    		{
    			A.id = B.id;
    		    A.mainClass = B.mainClass;
    		    A.subClass = B.subClass;
    			A.strReq = B.strReq;
    			A.chkReq = B.chkReq;
    		    A.quality = B.quality;
    		    A.bonding = B.bonding;
    		    A.price = B.price;
    		    A.stackSize = B.stackSize;
    		    A.primDmg = B.primDmg;
    		    A.sconDmg = B.sconDmg;
    		    A.resist = B.resist;
    	    	A.bnsOpt = B.bnsOpt;
    	        A.name = B.name;
    	        A.desc = B.desc;
    		    A.attSpeed = B.attSpeed;
    		    A.defSpeed = B.defSpeed;} */
    
    		void Buy(int jjj)
    		{
    			for (int iii=0; iii!=MAX_INV; iii++)
    				if (charInv[iii].id==0){ charInv[iii] = itemsDB[jjj]); break;}
    				else if (charInv[iii].id==itemsDB[jjj].id)
    					if (itemsDB[jjj].amount < itemsDB[jjj].stackSize){ itemsDB[jjj].amount++; break;}}
    
    		void ShowOpts()
    		{
    			string type;
    			for (int iii=0; iii!=10; iii++)
    				if (bnsOpt[iii]!=0)
    				{
    					if (iii==0)
    						type = "DEX";
    					else if (iii==1)
    						type = "STR";
    					else continue;
    					
    					cout << "Adds" << bnsOpt[iii] << type << endl;}}
    
    		void ShowInf2()
    		{
    			if (bonding==1) cout << "QUEST ITEM: Can't be sold, desstroyed and neither traded" << endl;
    			if (bonding==2) cout << "Can't be sold" << endl;}
    
    		void DescrWin()
    		{
    			/*showName();
    			showInf1();
    			showDesc();
    			showStts();*/
    			ShowOpts();
    			ShowInf2();}
    
    
    }itemsDB[MAX_ITEMS], charInv[MAX_INV], charEqup[10];
    ted: Project: Helios RPG - Beta, Configuration: Release Win32 ------
    Compiling...
    Main.cpp
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(163) : error C2059: syntax error : ')'
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(164) : error C2059: syntax error : 'else'
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(165) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(167) : error C2628: 'Items' followed by 'void' is illegal (did you forget a ';'?)
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(171) : error C2065: 'bnsOpt' : undeclared identifier
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(179) : error C2065: 'bnsOpt' : undeclared identifier
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(131) : warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(132) : warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(133) : warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(135) : warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(136) : warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(137) : warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(138) : warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(163) : error C2065: 'charInv' : undeclared identifier
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(163) : error C2228: left of '.id' must have class/struct/union
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(163) : error C2065: 'charInv' : undeclared identifier
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(163) : error C2065: 'itemsDB' : undeclared identifier
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(163) : error C2143: syntax error : missing ';' before '}'
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(163) : error C2043: illegal break
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(183) : error C2065: 'bonding' : undeclared identifier
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(184) : error C2065: 'bonding' : undeclared identifier
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(196) : error C2059: syntax error : '}'
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(196) : error C2143: syntax error : missing ';' before '}'
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Items.h(196) : error C2059: syntax error : '}'
    .\Main.cpp(10) : error C2065: 'charInv' : undeclared identifier
    .\Main.cpp(10) : error C2228: left of '.ID' must have class/struct/union
    Build log was saved at "file://c:\Documents and Settings\eng\My Documents\Visual Studio 2008\Projects\Project1\Helios RPG - Beta\Helios RPG - Beta\Release\BuildLog.htm"
    Helios RPG - Beta - 19 error(s), 7 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    because of that

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The errors has nothing to do with = and everything to do with you using names that don't exist, like charInv and itemsDB.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    My results:

    Code:
    Thank you for testing your code with Comeau C/C++!
    Tell others about http://www.comeaucomputing.com/tryitout !
    
    Your Comeau C/C++ test results are as follows:
    
    Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing.  All rights reserved.
    MODE:strict errors C++ C++0x_extensions
    
    "ComeauTest.c", line 43: warning: expression has no effect
      		strReq;
      		^
    
    "ComeauTest.c", line 44: warning: expression has no effect
      		chkReq;
      		^
    
    "ComeauTest.c", line 163: error: identifier "charInv" is undefined
      				if (charInv[iii].id==0){ charInv[iii] = itemsDB[jjj]); break;}
      				    ^
    
    "ComeauTest.c", line 163: error: identifier "itemsDB" is undefined
      				if (charInv[iii].id==0){ charInv[iii] = itemsDB[jjj]); break;}
      				                                        ^
    
    "ComeauTest.c", line 163: error: expected a ";" (perhaps on the previous statement)
      				if (charInv[iii].id==0){ charInv[iii] = itemsDB[jjj]); break;}
      				                                                    ^
    
    "ComeauTest.c", line 164: error: identifier "itemsDB" is undefined
      				else if (charInv[iii].id==itemsDB[jjj].id)
      				                          ^
    
    4 errors detected in the compilation of "ComeauTest.c".
    You are referring to global variables which have not been declared at this point.

    Using global variables itself is rather bad. Why not make the function a) static (since it doesn't affect this object), b) take the arrays to work with as arguments?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by anon View Post
    My results:

    Code:
    Thank you for testing your code with Comeau C/C++!
    Tell others about http://www.comeaucomputing.com/tryitout !
    
    Your Comeau C/C++ test results are as follows:
    
    Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing.  All rights reserved.
    MODE:strict errors C++ C++0x_extensions
    
    "ComeauTest.c", line 43: warning: expression has no effect
      		strReq;
      		^
    
    "ComeauTest.c", line 44: warning: expression has no effect
      		chkReq;
      		^
    
    "ComeauTest.c", line 163: error: identifier "charInv" is undefined
      				if (charInv[iii].id==0){ charInv[iii] = itemsDB[jjj]); break;}
      				    ^
    
    "ComeauTest.c", line 163: error: identifier "itemsDB" is undefined
      				if (charInv[iii].id==0){ charInv[iii] = itemsDB[jjj]); break;}
      				                                        ^
    
    "ComeauTest.c", line 163: error: expected a ";" (perhaps on the previous statement)
      				if (charInv[iii].id==0){ charInv[iii] = itemsDB[jjj]); break;}
      				                                                    ^
    
    "ComeauTest.c", line 164: error: identifier "itemsDB" is undefined
      				else if (charInv[iii].id==itemsDB[jjj].id)
      				                          ^
    
    4 errors detected in the compilation of "ComeauTest.c".
    You are referring to global variables which have not been declared at this point.

    Using global variables itself is rather bad. Why not make the function a) static (since it doesn't affect this object), b) take the arrays to work with as arguments?
    i don't get the static part

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Imagine that you are learning to build a house. You have been told how to saw and how to hammer in nails. Would you then start building a shed? Or do you think you need to learn a few more things first? Perhaps practice by building some really simple constructions.

    You are currently building a shed with the two tools you know. You do not have the foundations in tools and techniques to build a LARGE program. You end up with "stupid" solutions, because you don't know of a better solution.

    If you keep going, you will end up with a big mess. If you instead spend some time practicing on small excercises, something that ends up with 20-50 lines of code, then something that is complete in 200 lines, etc, etc, you will learn much more - because there is much less "do things over and over again" work, which you get in a larger project.

    One thing I'm pretty sure you need to learn is how to compose several objects into a larger object. That would solve several of your problems, including the 32 arguments to one function - it is a TYPICAL "I don't know how to make a simple object to hold these things, so I will just pass the individual items along" beginner solution to "I need to pass all these things to this function".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21