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() ) - int deal() {
-
-
int dealcard = cards.front();
-
cards.erase(cards.begin());
-
return dealcard;
-
} //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!
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() ) - int deal() {
-
-
int dealcard = cards.front();
-
cards.erase(cards.begin());
-
return dealcard;
-
} //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
At the top of the program after my #include statements I have
class cards;
cards is basically a vector created by the shuffle function -
const int SIZE = 52;
-
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};
-
std::vector < int > cards(a1, a1 + SIZE);
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
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 - class cards;
-
-
void shuffle() {
-
-
const int SIZE = 52;
-
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};
-
std::vector < int > cards(a1, a1 + SIZE);
-
std::ostream_iterator< int > coutput( cout, " ");
-
-
random_shuffle(cards.begin(), cards.end());
-
} //End function shuffle
-
-
int deal() {
-
-
int dealcard = cards.front();
-
cards.erase(cards.begin());
-
return dealcard;
-
} //End function deal
Thanks for the help.
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.
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. - #include <iostream>
-
#include <vector>
-
#include <algorithm>
-
#include <iterator>
-
using namespace std;
-
-
class cards;
-
-
const int SIZE = 52;
-
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};
-
std::vector < int > cards(a1, a1 + SIZE);
-
-
-
-
void shuffle() {
-
-
random_shuffle(cards.begin(), cards.end()); //Shuffle cards
-
} //End function shuffle
-
-
int deal() {
-
-
int dealcard = cards.front(); //Deal first card from deck
-
cards.erase(cards.begin()); //Remove first card from deck
-
return dealcard; //Output total of card delt
-
} //End function deal
-
-
int player() {
-
-
int firstcard = deal(); //Get first card for blackjack
-
int secondcard = deal(); //Get second card for blackjack
-
int sumcards = firstcard + secondcard; //Then add the total of the two cards to see
-
while(sumcards < 17) { //If score is less than 17
-
int nextcard = deal(); //If it is get another card
-
int sumcards = sumcards + nextcard; //And see if the sum is still less than 17
-
} //If it is still less than 17 loop and get another card
-
return sumcards; //If the score is 17 or more, output the total score
-
} //End function player
-
-
-
int main(){ //Start main function
-
-
int counter = 0; //counter for number of loops
-
int i, results, total; //declare variables
-
-
vector<int>scores; //Create vector to hold scores of deals
-
ostream_iterator<int>output(cout, " ");
-
-
while (counter<100000){ //Start loop, conitune 100000 times
-
counter = counter + 1; //Increase counter by 1 each loop
-
-
shuffle(); //Shuffle the cards
-
total = player(); //Take cards and find final value of all cards
-
-
scores.push_back(total); //Add scores to vector
-
}
-
-
while(i=0, i<31, i++){
-
results = count(scores.begin(), scores.end(), i);
-
cout << results << ": Number of times scored " << i << endl;
-
}
-
-
return 0;
-
}
-
Thanks for the help once again.
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.
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.
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?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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...
|
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
|
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.
| |
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...
|
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;
|
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...
|
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 ());
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |