Thread: C++ compile error, help please

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

    C++ compile error, help please

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Races
    {
    public:
    	int raceID;
    	string raceName;
    	string raceEner;
    	string raceEP;
    	string raceDesc;
    	float raceAttG[4];
    	float raceResE[3];
    	float raceResP[3];
    	float raceResM[3];
    
    	Races ();
    	Races (int a, string b, string c, string d, string e, float f, float g, float aa, float ab, float ac, float ad, 
    		float ae, float af, float ag, float ba, float bb, float bc, float bd)
    	{
    		raceID = a;
    	    raceName = b;
    	    raceEner = c;
    	    raceEP = d;
    	    raceDesc = e;
    		raceAttG[0] = f;
    		raceAttG[1] = g;
    		raceAttG[2] = aa;
    		raceAttG[3] = ab;
    	    raceResE[0] = ac;
    		raceResE[1] = ad;
    		raceResE[2] = ae;
    	    raceResP[0] = af;
    		raceResP[1] = ag;
    		raceResP[2] = ba;
    	    raceResM[0] = bb;
    		raceResM[1] = bc;
    		raceResM[2] = bd;
    	}
    }DBraces[20], charRace;
    
    int main()
    {
    	DBraces[1].Races(1, "a", "a", "a", "a", 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5);
    
    	return 0;
    }

    THE ERROR
    ------ Build started: Project: Helios RPG - Beta, Configuration: Release Win32 ------
    Compiling...
    Main.cpp
    .\Main.cpp(45) : error C2274: 'function-style cast' : illegal as right side of '.' operator
    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 - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    whats wrong with it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ExDHaos
    Races (int a, string b, string c, string d, string e, float f, float g, float aa, float ab, float ac, float ad,
    float ae, float af, float ag, float ba, float bb, float bc, float bd)
    Oh dear, that is... excessive.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by laserlight View Post
    Oh dear, that is... excessive.
    well thats a database class after all >.>

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by laserlight View Post
    Oh dear, that is... excessive.
    but u don't think its the problem, do u?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ExDHaos
    well thats a database class after all >.>
    So?

    Quote Originally Posted by ExDHaos
    but u don't think its the problem, do u?
    It is a problem, but it is not the reason for your compile error. The reason is that you appear to be trying to call a constructor as if it were a normal member function:
    Code:
    DBraces[1].Races(1, "a", "a", "a", "a", 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5);
    Anyway, DBraces[1] has already been default constructed by that point.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by laserlight View Post
    So?


    It is a problem, but it is not the reason for your compile error. The reason is that you appear to be trying to call a constructor as if it were a normal member function:
    Code:
    DBraces[1].Races(1, "a", "a", "a", "a", 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5);
    Anyway, DBraces[1] has already been default constructed by that point.
    well im really new to coding, is there a better way to make a database class or whatever?

    and how can i fix it then, is there a way to force the array not to use the defult constructor (it always gave me an erroe if i didn't use one) but well, thx for pointing that out btw

  7. #7
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    You could make an initialising member function so that the constructor can call it, and you can call it later if you want to change something.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ExDHaos
    well im really new to coding, is there a better way to make a database class or whatever?
    Just as you have made use of arrays as member variables, you can make use of arrays for parameters.

    Quote Originally Posted by ExDHaos
    and how can i fix it then, is there a way to force the array not to use the defult constructor (it always gave me an erroe if i didn't use one) but well, thx for pointing that out btw
    Unfortunately, this is not easy to do by hand. If you are allowed and willing to use a std::vector<Races>, you can then construct a dynamic array of Races objects with your desired constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by yaya View Post
    You could make an initialising member function so that the constructor can call it, and you can call it later if you want to change something.
    could ya show me plz? i didn't get it xP

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by laserlight View Post
    Just as you have made use of arrays as member variables, you can make use of arrays for parameters.


    Unfortunately, this is not easy to do by hand. If you are allowed and willing to use a std::vector<Races>, you can then construct a dynamic array of Races objects with your desired constructor.
    is there another way i can modify the array data?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    yaya's suggestion turns construction into a two phase process. Generally, I do not recommend such an approach as I feel that construction should be a single process, either by directly using a constructor, or by some factory function.

    Yet, it may well be the case that you are not looking to construct the objects with the given data. Maybe you are actually looking to default construct the objects, and then assign the appropriate data to them. Perhaps some setter functions are appropriate?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Laserlight is right, but this is how I'd do the class:

    Code:
    	void Init (int a, string b, string c, string d, string e, float f, float g, float aa, float ab, float ac, float ad, 
    		float ae, float af, float ag, float ba, float bb, float bc, float bd)
    	{
    	    raceID = a;
    	    raceName = b;
    	    raceEner = c;
    	    raceEP = d;
    	    raceDesc = e;
    	    raceAttG[0] = f;
    	    raceAttG[1] = g;
    	    raceAttG[2] = aa;
    	    raceAttG[3] = ab;
    	    raceResE[0] = ac;
    	    raceResE[1] = ad;
    	    raceResE[2] = ae;
    	    raceResP[0] = af;
    	    raceResP[1] = ag;
    	    raceResP[2] = ba;
    	    raceResM[0] = bb;
    	    raceResM[1] = bc;
    	    raceResM[2] = bd;
    	}
    
    	Races ()
    	{
    		Init (1, "a", "a", "a", "a", 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5);
    	}
    So then you would simply construct your object, and if you didn't like the way it turned out, you can call Init() again, though directly for the second time. The main problem with this is that you are initializing everything twice, which is never good and will be slow if you do it heaps. But it is an option.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by yaya View Post
    Laserlight is right, but this is how I'd do the class:

    Code:
    	void Init (int a, string b, string c, string d, string e, float f, float g, float aa, float ab, float ac, float ad, 
    		float ae, float af, float ag, float ba, float bb, float bc, float bd)
    	{
    	    raceID = a;
    	    raceName = b;
    	    raceEner = c;
    	    raceEP = d;
    	    raceDesc = e;
    	    raceAttG[0] = f;
    	    raceAttG[1] = g;
    	    raceAttG[2] = aa;
    	    raceAttG[3] = ab;
    	    raceResE[0] = ac;
    	    raceResE[1] = ad;
    	    raceResE[2] = ae;
    	    raceResP[0] = af;
    	    raceResP[1] = ag;
    	    raceResP[2] = ba;
    	    raceResM[0] = bb;
    	    raceResM[1] = bc;
    	    raceResM[2] = bd;
    	}
    
    	Races ()
    	{
    		Init (1, "a", "a", "a", "a", 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5);
    	}
    So then you would simply construct your object, and if you didn't like the way it turned out, you can call Init() again, though directly for the second time. The main problem with this is that you are initializing everything twice, which is never good and will be slow if you do it heaps. But it is an option.
    thanks, ur suggestion gave me another idea

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Races
    {
    public:
    	int raceID;
    	string raceName;
    	string raceEner;
    	string raceEP;
    	string raceDesc;
    	float raceAttG[4];
    	float raceResE[3];
    	float raceResP[3];
    	float raceResM[3];
    
    	Races ();
    	void raceInfo (int iValue, string sString[4], float dValue[13])
    	{
    		raceID = iValue;
    	    raceName = sString[0];
    	    raceEner = sString[1];
    	    raceEP = sString[2];
    	    raceDesc = sString[3];
    		raceAttG[0] = dValue[0];
    		raceAttG[1] = dValue[1];
    		raceAttG[2] = dValue[2];
    		raceAttG[3] = dValue[3];
    	    raceResE[0] = dValue[4];
    		raceResE[1] = dValue[5];
    		raceResE[2] = dValue[6];
    	    raceResP[0] = dValue[7];
    		raceResP[1] = dValue[8];
    		raceResP[2] = dValue[9];
    	    raceResM[0] = dValue[10];
    		raceResM[1] = dValue[11];
    		raceResM[2] = dValue[12];
    	}
    }DBraces[20], charRace;
    
    int main()
    {
    	DBraces[1].raceInfo(1, "a", "a", "a", "a", 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5);
    
    	return 0;
    }
    but now it gives a different error
    ------ Build started: Project: Helios RPG - Beta, Configuration: Release Win32 ------
    Compiling...
    Main.cpp
    .\Main.cpp(44) : error C2660: 'Races::raceInfo' : function does not take 18 arguments
    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 - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    does it really mean that i can't put 18 arguments? =/
    Last edited by ExDHaos; 05-14-2009 at 02:37 PM.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    several of your arguments expect arrays. you are not sending any arrays. you have to create the arrays first and then send each array as a single parameter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 02:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 01:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 08:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM