473,626 Members | 3,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems compiling program (expected primary-expression, unqualified-id)

6 New Member
I have been working on a program and have been unable to resolve these compile issues.

g++ main.cpp -o project5.exe
main.cpp: In function 'int deal()':
main.cpp:20: error: expected primary-expression before '.' token
main.cpp:21: error: expected unqualified-id before '.' token


Inside program main is this (outside of main() )

Expand|Select|Wrap|Line Numbers
  1. int deal() {
  2.  
  3.   int dealcard = cards.front();
  4.   cards.erase(cards.begin());
  5.   return dealcard;
  6. } //End function deal
Where line 3 in the code posted relates to line 20 in the error part above.

I know this is only a small part of the program, but perhaps someone could tell me my problem without me having to post up the entire code.

Thanks a bunch!
Mar 12 '08 #1
10 2707
gpraghuram
1,275 Recognized Expert Top Contributor
I have been working on a program and have been unable to resolve these compile issues.

g++ main.cpp -o project5.exe
main.cpp: In function 'int deal()':
main.cpp:20: error: expected primary-expression before '.' token
main.cpp:21: error: expected unqualified-id before '.' token


Inside program main is this (outside of main() )

Expand|Select|Wrap|Line Numbers
  1. int deal() {
  2.  
  3.   int dealcard = cards.front();
  4.   cards.erase(cards.begin());
  5.   return dealcard;
  6. } //End function deal
Where line 3 in the code posted relates to line 20 in the error part above.

I know this is only a small part of the program, but perhaps someone could tell me my problem without me having to post up the entire code.

Thanks a bunch!

Can you tell me how u have declared the variable cards

Raghuram
Mar 12 '08 #2
tnwagn
6 New Member
At the top of the program after my #include statements I have

class cards;

cards is basically a vector created by the shuffle function
Expand|Select|Wrap|Line Numbers
  1. const int SIZE = 52;
  2.   int a1 [SIZE]= {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8\
  3. ,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10};
  4.   std::vector < int > cards(a1, a1 + SIZE);
Mar 12 '08 #3
gpraghuram
1,275 Recognized Expert Top Contributor
cards is a class and the front() method you are calling belongs to the vector.
Or do u have a method front() in the call cards?

Raghu
Mar 12 '08 #4
tnwagn
6 New Member
What I am doing with theses is creating a vector of 52 cards and then shuffling them in the first function "shuffle()" in this function I create the vector "cards". In the second function "deal" I take the first element of the vector "cards" with the command .front() (which are available for all swqunece containers). I then erase the first element of the vector with .erase (available in first class containers).

When attempting to compile I get the errors mentioned above.

Here is that includes those parts

Expand|Select|Wrap|Line Numbers
  1. class cards;
  2.  
  3. void shuffle() {
  4.  
  5.   const int SIZE = 52;
  6.   int a1 [SIZE]= {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8\
  7. ,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10};
  8.   std::vector < int > cards(a1, a1 + SIZE);
  9.   std::ostream_iterator< int > coutput( cout, " ");
  10.  
  11.   random_shuffle(cards.begin(), cards.end());
  12. } //End function shuffle
  13.  
  14. int deal() {
  15.  
  16.   int dealcard = cards.front();
  17.   cards.erase(cards.begin());
  18.   return dealcard;
  19. } //End function deal
Thanks for the help.
Mar 12 '08 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
class cards;
You are using a forward reference.

Question: How the the compiler know there is a front() method.

Answer: It doesn't.

There's your error.

A forward reference is enough for the compiler to allow a pointer to your class but if you do more than that, you need to include the class declaration.
Mar 12 '08 #6
tnwagn
6 New Member
Thanks for the help everyone. I, with the help of some friends, been able to compile the program with no errors. However, I am now getting Segmentation Fault when I try and run the program. This is, from what I've read, when the program calls areas of memory it doesn't have acess to. So here is the entire thing. Let me know if you can find any reason why it is returning a segmentation fault.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5. using namespace std;
  6.  
  7. class cards;
  8.  
  9.   const int SIZE = 52;
  10.   int a1 [SIZE]={1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10};
  11.   std::vector < int > cards(a1, a1 + SIZE);
  12.  
  13.  
  14.  
  15. void shuffle() {
  16.  
  17.   random_shuffle(cards.begin(), cards.end()); //Shuffle cards
  18. } //End function shuffle
  19.  
  20. int deal() {
  21.  
  22.   int dealcard = cards.front(); //Deal first card from deck
  23.   cards.erase(cards.begin()); //Remove first card from deck
  24.   return dealcard; //Output total of card delt
  25. } //End function deal
  26.  
  27. int player() {
  28.  
  29.   int firstcard = deal();                //Get first card for blackjack
  30.   int secondcard = deal();               //Get second card for blackjack
  31.   int sumcards = firstcard + secondcard; //Then add the total of the two cards to see
  32.   while(sumcards < 17) {                 //If score is less than 17
  33.     int nextcard = deal();               //If it is get another card
  34.     int sumcards = sumcards + nextcard;  //And see if the sum is still less than 17
  35.   }                                      //If it is still less than 17 loop and get another card
  36.   return sumcards;                       //If the score is 17 or more, output the total score
  37. } //End function player
  38.  
  39.  
  40. int main(){ //Start main function
  41.  
  42.   int counter = 0;  //counter for number of loops
  43.   int i, results, total;   //declare variables
  44.  
  45.   vector<int>scores; //Create vector to hold scores of deals
  46.   ostream_iterator<int>output(cout, " ");
  47.  
  48.   while (counter<100000){  //Start loop, conitune 100000 times
  49.     counter = counter + 1; //Increase counter by 1 each loop
  50.  
  51.     shuffle(); //Shuffle the cards
  52.     total = player(); //Take cards and find final value of all cards
  53.  
  54.     scores.push_back(total); //Add scores to vector
  55.   }
  56.  
  57.   while(i=0, i<31, i++){
  58.       results = count(scores.begin(), scores.end(), i);
  59.       cout << results << ": Number of times scored " << i << endl;
  60.   }
  61.  
  62.   return 0;
  63. }
  64.  
Thanks for the help once again.
Mar 12 '08 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
Your deal() function is erasing elements from the vector. That's OK if there are elements to erase. Otherwise: KA-BOOM.

I don't see any provision for checking this.

Also, your class cards is pointless. Did you know that the vector has the same name?

Please read this article http://www.thescripts.com/forum/thread737451.html.
Mar 12 '08 #8
tnwagn
6 New Member
Please read this article http://www.thescripts.com/forum/thread737451.html.
After reading this it sounds like you would want me (and it would be better for me) to, instead of having the vector "cards" be global, put it inside of a function. This is the way I originally had it set up. However, my other functions inside of the program (mostly deal() ) where unable to acess the vector when it was inside of the shuffle() function. So would you say to put the vector "cards" definition inside of it's own function and then have all the others call that function instead of the vector?

Your deal() function is erasing elements from the vector. That's OK if there are elements to erase. Otherwise: KA-BOOM.

I don't see any provision for checking this.
This program is supposed to run through the shuffle,deal, get a hand routine 100,000 times. What is happening then I guess is that the vector of "cards" is never being reinitilized with 52 cards so after the first few runs it runs out of cards and, as you say, goes ka-boom. I imagine that it would work if it only had to loop around 20 times.


Also, your class cards is pointless. Did you know that the vector has the same name?
Yes, I knew that cards was the same for the class name as well as the vector name. However, I do not completely understand the entire "class" thing. From what I've gathered so far is that you have to define the vector as some sort of class. I guess that is horribly wrong. I will take the line involving class out and see if that works.
Mar 12 '08 #9
tnwagn
6 New Member
I still have the Segmentation error when I try and run the program. I have no idea exactly why it is coming up but I do know of at least one problem.

The "deck" is created at the beginning globally. This is so that every other function can acess it. However, I've been told that's wrong.

But I don't understand how to put a vector into a function and still have other functions be able to call that vector and perform operations on it.

I really to find a solution to this. Can someone help me out?
Mar 13 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1910
by: Steve Jenkins | last post by:
Hi all, I'm trying to install Sablotron on Red Hat Linux release 7.2 (Enigma). I've got Sablotron to compile. I now get the following error when running ./configure on PHP: checking for Sablotron libraries in the default path... found in /usr/local checking for sablot-config... found checking for Sablotron version... configure: error: Sablotron version
5
1883
by: jose luis fernandez diaz | last post by:
Hi, When I compiling the program below: #include <map> using namespace std; template<typename td1, typename td2, typename td3,typename td4> class Tarificador
2
2112
by: Erik | last post by:
Hi Everyone, I'm having real problems compiling some source for eVC4++. The errors I am getting are below: It all seems to be centred around winsock. If I move the afsock.h reference to before my other includes then I get lots of errors like C2011: 'fd_set' : 'struct' type redefinition warning C4005: 'FD_CLR' : macro redefinition which I understand are due to the fact that windows.h is being included in another header file as well as...
3
2315
by: Ryan Riehle | last post by:
Hi All! Trying to upgrade to Apache 2.0.49 and getting compile errors related to mod_auth_pgsql, any clue?: make: Entering directory `/usr/src/httpd-2.0.49' /usr/src/httpd-2.0.49/srclib/apr/libtool --silent --mode=link gcc -pthread -I/ =500 -D_BSD_SOURCE -D_SVID_SOURCE -D_GNU_SOURCE -DAP_HAVE_DESIGNATED_INITIALIZER I. -I/usr/src/httpd-2.0.49/os/unix -I/usr/src/httpd-2.0.49/server/mpm/prefork -I .49/modules/proxy
1
2454
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and installed the boost library. This is using gcc under FC3.
6
2591
by: Josefo | last post by:
Hello all. I am a newbie following the C++ tutorial in : http://www.cplusplus.com/doc/tutorial/templates.html I am unable to succesfully compile any of the examples with templates of this tutorial. I use the standard c++ compiler which comes with ubuntu breezy distro. I guess that somethig is wrong with it or (more likely..) I should use some option when compiling. This is, for instance, one of the codes: // template specialization...
2
11273
by: renagade629 | last post by:
Can anybody help me understand what i'm doing wrong or what I'm missing? Is there anyother good and commendable C++ program I can use (free) from the internet like Dev C++? I'm having trouble doing basic compiling on this new Dev C++, when i try to print something like: #include<iostream.h> using namespace std; int main () { cout << "Hello to the world" << endl; system("PAUSE") return 0;
6
2254
by: msb_6 | last post by:
Currently I have a PHP extension thats all written and compiles under windows, but the PC I'm going to end up putting it on is running Ubuntu 8.04 (g++ 4.2.3). I've delved into PHP documentation and ended up with a simple config.m4 file and the process to compile. However when I try to compile I get loads of errors that I didn't get when compiling under windows. I've managed to simplify the code down and create a simple extension (that...
1
4509
by: BobLewiston | last post by:
I tried to compile a Windows Forms Application in Visual C# 2008 Express with this source code from the CSharpSchool tutorial at Programmer's Heaven:using System; using System.Windows.Forms; using System.Drawing; namespace CSharpSchool { class HelloWinForm { static void Main () { Application.Run (new MyWindow ());
0
8701
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6122
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1507
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.