Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
So, the title is the error code that I get, and I am not experienced enough to see where the problem lies. I have quite a bit of code here that I would love to get some help with too, see where the problem lies.
main.cpp - the rest of the main code is as usual, so nothing wrong underneath the photo
card.cpp
card.h - part one
card.h - part two
cardDeck.h
cardDeck.cpp
I don't really know what to do here, but I have tried to see if I have any sort of syntax errors.
The compiler is complaining that it can't construct a Card object. There are only 2 places in your code where a Card object is being constructed:
In main(). You are constructing a Card object correctly. So this is not the problem.
In CardDeck's constructor. You are constructing 52 default Card objects in your cardVector (this is the problem!), and then replacing them with new Cards in a loop.
The replacement Cards are being constructed correctly. However, cardVector.resize() requires Card to be default-constructible when adding new elements to the vector. But Card is not default-constructible, because it has only 1 user-defined constructor which requires a Suit and a Rank as input (thus the compiler-generated default constructor is disabled), and you are not providing a default constructor of your own.
So, you need to either:
give Card a default constructor, eg:
class Card {
private:
Suit s;
Rank r;
public:
Card() : s(), r() {} // <-- HERE
Card(Suit suit, Rank rank) : s(suit), r(rank) {}
...
};
Or:
class Card {
private:
Suit s;
Rank r;
public:
Card() : Card(Suit(), Rank()) {} // <-- HERE
Card(Suit suit, Rank rank) : s(suit), r(rank) {}
...
};
Or:
class Card {
private:
Suit s{};
Rank r{};
public:
Card() = default; // <-- HERE
Card(Suit suit, Rank rank) : s(suit), r(rank) {}
...
};
use the std::vector's push_back() or emplace_back() method instead of the resize() method when populating the cardVector with new Cards, eg:
CardDeck::CardDeck(){
//cardVector.resize(52);
cardVector.reserve(52);
//int index = 0;
for (int suit = 0; suit < 4; suit++){
for (int rank = 0; rank < 13; rank++){
//cardVector.at(index) = Card(static_cast<Suit>(suit), static_cast<Rank>(rank));
cardVector.push_back(Card(static_cast<Suit>(suit), static_cast<Rank>(rank)));
//or:
cardVector.emplace_back(static_cast<Suit>(suit), static_cast<Rank>(rank));
//index++;
}
}
}
That being said, since a deck has a fixed 52 Cards, you might consider using a fixed-sized array (or std::array) instead of std::vector.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
So, I know a vector object can be declared and initialized like this:
// Nothing new here. I know <int> is a template
// and how to implement it:
vector <int> vect{ 10, 20, 30 };
I assume that the vector object has inside an array of values, and the functions of that class (like push_back() for example) manage it. I would like and have been trying to implement something like that in a class of my own, without success. Would be interesting being able to understand how it's done! Did many "experiments" but none worked.
// Random named class:
class A_Class
{
private:
// A pointer for the type I want:
int *A_pointer_To_int;
public:
// Trying to accept the input values between
// brackets and putting them inside a temp array:
A_Class(int Input_Array[]) {}
};
int main()
{
// trying to create the object like in the vector class.
// Returns error "No instance of constructor matches the argument list":
A_Class My_Object{1,2,3}
return 0;
}
In a function parameter, int Input_Array[] is just syntax sugar for a decayed pointer int* Input_Array, which does not provide any information about any array that may be passed in to it.
For what you are attempting, you need to accept a std::initializer_list instead, eg:
#include <initializer_list>
#include <algorithm>
// Random named class:
class A_Class
{
private:
// A pointer for the type I want:
int *A_pointer_To_int;
// the number of values in the array:
size_t size;
public:
A_Class(std::initializer_list<int> Input_Values) {
size = Input_Values.size();
A_pointer_To_int = new int[size];
std::copy(Input_Values.begin(), Input_Values.end(), A_pointer_To_int);
}
~A_Class() {
delete[] A_pointer_To_int;
}
};
int main()
{
A_Class My_Object{1,2,3}; // works now
return 0;
}
Online Demo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I dont exactly get the use of a constructor
For example:
class car
{
public:
string name;
long int price;
int mileage
bool ownedByPlayer;
};
Here, why will I use a constructor like this,
class car
{
public:
string name;
long int price;
int mileage
bool ownedByPlayer;
car()
{
ownedByPlayer = false;
}
};
if I can simply define the variable ownedByPlayer where I have declared it. Like: bool ownedByPlayer = false; ?
In your case there is no need to write the constructor. Actually it is recommended to not write a constructor that does nothing but initialize members with default values. Though the correct way would be to use the member initializer list:
car() : ownedByPlayer(false) {}
And since C++11 you can use a default member initializer instead:
class car {
// ...
bool ownedByPlayer = false;
// ...
};
Sometimes constructors actually need to do something. Consider a car registers itself somewhere after being constructed, then your class could look like this:
class car
{
public:
string name;
long int price;
int mileage;
car(string name,long int price, int mileage) : name(name),price(price),mileage(mileage) {
CarRegistrationFacility::register_car(*this);
}
};
Actually I found it difficult to make up an example for a constructor that does more than initializing members, because thats what constructors do. However, when your constructor does more than that, then that "more" happens in the body of the constructor.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am really struggling with using functions and classes, and on VS, I get a "Matrix::Matrix()" is unaccessible error, and I just cannot figure out why.
I'm trying to learn about functions and classes and just am not getting it! The program basically uses a constructor and destructor in a class called Matrix, and creates a 'matrix'. I am now trying to use a function within this matrix class to get the value from inside of the constructed Matrix, and it will return the correct value, but don't understand why I am getting this error.
#include <iostream>
using namespace std;
class Matrix
{
Matrix();
private:
int M;
int N;
double *data;
int get(int i, int j){
return data[i*N+j];
}
//CONSTRUCTOR
public:
Matrix(int sizeR, int sizeC,double * input_data)
{
M=sizeR;//Rows
N=sizeC;//Columns
data = new double[M*N];//creation of 1D array, uses m&n values
cout<<"\nMatrix::Matrix(int sizeR, int sizeC, double * input_data) is invoked\n\n";
//ENTER DATA INTO MATRIX HERE:
for(int i=0;i<M*N;i++)//Loops for every data entry into 1D array, uses r&c as referenece to size
data[i] = input_data[i];//Accesses each value at specific location, inputs value 'val'
for(int i=0;i<M*N;i++)//Loops for every data entry into 1D array
cout<<data[i]<<" ";
}
//DECONSTRUCTOR
~Matrix(){
//delete data
delete []data;
cout<<"\n\nMatrix::~Matrix() is invoked\n\n";
}
};
int main()
{
int sizeR, sizeC, g1, g2;
g1 = 2;
g2 = 2;
Matrix M1;
cout<<"Enter No. Rows: ";
cin>>sizeR;
cout<<"Enter No. Columns: ";
cin>>sizeC;
double * input_data;
input_data = new double[sizeR*sizeC];
//INPUTS VALUES TO ARRAY
for(int i=0;i<sizeR*sizeC;i++)//Loops for every row
input_data[i] = i+1*input_data[i-1];
Matrix(sizeR, sizeC, input_data);
cout<<"Find value at row: ";
cin>>sizeR;
cout<<"Find value at column: ";
cin>>sizeC;
M1.get(g1, g2);
}
The default access modifier for classes in C++ is private so if you didn't explicitly used the public: access modifier before the definition of the constructor Matrix() then it would be considered to be private.
You want to create M1 object using the constructor you provided.
The line Matrix(sizeR, sizeC, input_data); doesn't create M1 object.
You need to do Matrix M1(sizeR, sizeC, input_data); or create the default constructor with public accessibility.
Furthermore (as somebody pointed out) M1.get(...) will also cause errors if Matrix::get() is not public.
Just place your default constructor (declaration/definition) in your class' public section:
class Matrix {
public: // <<<<
Matrix() {
}
Matrix(int sizeR, int sizeC,double * input_data) {
// ...
}
// ...
};
The default scope policy for class is private unless specified differently.
There is no definition of the constructor which takes no parameters, and further its declaration is private. Therefore, the call
Matrix M1;
fails. Just provide a definition in your class such as
Matrix() {}
and make that public accessible and it will work fine.
EDIT: The get function as well need to be public. Next, this raw array is a bad thing which leeds to a double-free corruption error. Use a std::vector instead. This becomes code-review, I guess. See here for a still quick-n-dirty but somewhat improved implementation.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have tried to make some sort of card game here but the classes for the holder / cardpile don't give away right things to the reciver/player and I don't know how to sort this out.
Here is the program:
#include <iostream>
using namespace std;
class subclass // like a card
{
public:
subclass();
subclass(int x);
int nummer;
int getnummer();
};
subclass::subclass():nummer(0){}
subclass::subclass(int x):nummer(x){}
int subclass::getnummer()
{
return nummer;
}
holder class:
class holder // like a cardpile
{
public:
holder();
subclass cardpile[2];
subclass* pile; // a cardpile which has 2 cards, with nummber 1 and 2.
subclass* getsubclass(int i); // take first or second card.
};
holder::holder()
{
subclass first(1*3);
subclass second(2*3);
pile=cardpile;
pile=&first;
pile++;
pile=&second;
pile--;
}
subclass* holder::getsubclass(int i) //1 eller 2.
{
return pile+i;
}
reciver class/ the player
class reciver // like a player
{
public:
subclass part_of_holder;
reciver();
void getsubclass( subclass* in); // give this card to player from this pointer
void showinside(); // what card do player have
};
reciver::reciver():part_of_holder(){}
void reciver::getsubclass( subclass* in)
{
part_of_holder=*in;
}
void reciver::showinside() // what card do player have
{
cout<< part_of_holder.getnummer();
}
and the main
int main()
{
holder cardpile;
reciver player;
cout << "first card should be 1*3 and it is : " ;
player.getsubclass(cardpile.getsubclass(0));
player.showinside();
cout << endl <<"first card should be 2*3 and it is : " ;
player.getsubclass(cardpile.getsubclass(1));
player.showinside();
return 0;
}
In the end all I get is nonsence
holder::holder()
{
subclass first(1*3);
subclass second(2*3);
pile=cardpile;
pile=&first; // WRONG
pile++;
pile=&second; // WRONG
pile--;
}
The two lines above dont do what I suspect you think they do.
If you are trying to assign in the cardpile you would need something more like this:
cardpile[0] = first or *pile = first.
Sadly they wont work, since you dont have copy constructors. You also dont have any way to set a subclass's nummer field once constructed, so you are slightly doomed.
Add a setter function to the get instruction, and you could write this:
cardpile[0].setnummer(first.getnummer());
This is not going to work
holder::holder()
{
subclass first(1*3); // create temporary
subclass second(2*3); // create another temoporary
pile=cardpile; // assign member pointer to another member!!!
pile=&first; // assign member pointer to temporary
pile++;
pile=&second; // assign member pointer to temporary
pile--;
}
// member pointer pile points to object that no longer exists!!!
Basically once you create an instance of holder its internal pile pointer points to an object that no longer exists and that alone makes your class unstable because your getsubclass method returns an offset of this pointer - thus any attempt to dereference it will likely segfault.
In all honesty you would be better to start off again from scratch but a way of ensuring that you assign these temporaries to your member array would be
cardpile[0] = subclass(1*3);
cardpile[1] = subclass(2*3);
pile = cardpile;
however, the way this code is written is not a good design and I would advise starting from scratch when your grasp of C++ is better.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Doing a programming homework assignment and I am having some trouble with pointers. I'm not too sure what the issue is.
I have looked around and found a few solved issues but I can't seem to figure out how to implement the fixes in my own code. (noob)
in my main I call:
MotherShip* m1 = new MotherShip(5, 6);
I am getting the error "cannot instantiate abstract class" with this.
MotherShip.h:
#include "SpaceShip.h"
class MotherShip : public SpaceShip
{
public:
int capacity;
MotherShip();
MotherShip(int x, int y, int cap);
MotherShip(const MotherShip& ms);
void print();
};
MotherShip.cpp:
#include "stdafx.h"
#include "MotherShip.h"
MotherShip::MotherShip()
{
}
MotherShip::MotherShip(int x, int y, int cap)
{
}
MotherShip::MotherShip(const MotherShip& ms)
{
}
void MotherShip::print()
{
}
Here is my full main (I don't think it's important here so I thought I'd just pastie it)
http://pastie.org/pastes/8429256/text
You're passing two arguments to your class constructor, however you have not defined a constructor that takes two arguments.
One solution would be:
MotherShip* m1 = new MotherShip(5, 6, 7 /* passing third argument */);
Another solution is defining a constructor to take two arguments:
MotherShip(int x, int y);
you must set the cap parameter as your constructor requires it.
There's no constructor that takes two ints!
Use the default value in your declaration
MotherShip(int x, int y, int cap = 123);
or, as an alternative, declare and define another constructor that takes two ints:
MotherShip(int x, int y);
Could be guessed without looking. abstract class in C++ is implemented by adding a pure virtual function.
You sure have a pure virtual function in your base class SpaceShip which you need to override in MotherShip. Or else MotherShip too becomes abstract and cannot be instantiated.
class SpaceShip
{
public:
virtual void DoSomething() = 0; //override this with some implementation in MotherShip
};