Pointer Issues "cannot instantiate abstract class" [closed] - c++

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
};

Related

why is the const in the signature allowing the change in address of variable 'a' [closed]

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 9 months ago.
Improve this question
why is the const in the void change(int newValue,int *&what) const allowing change in address of 'a' when pointer to it is passed as ref. but this function is supposed to keep data of 'this' const. please help me where i am lacking my concept.
Note : please see comments in private change function..
#include <iostream>
using namespace std;
class A{
private:
int *a;
void change(int newValue,int *&what) const{
/*
//when what received as 'int * what'
a=new int(25);//not ok
what=new int(25);//ok as it does not change 'a'
*(this->a)=newValue;//ok
*what=newValue;//ok
*/
//when what received as 'int *&what'
//a=new int(25);//not ok
delete a;
what=new int(25);//ok but how ?
//*(this->a)=newValue;//ok
//*what=newValue;//ok
}
public:
A(){
a=new int(10);
}
void change(int newValue){
change(newValue,a);
}
void show(){
cout<<*a<<endl;
}
};
int main(){
A a;
a.change(15);
a.show();
return 0;
}
In this method:
void change(int newValue){
change(newValue,a);
}
... passing a into change(int, int*&) is allowed, because change(int) is not tagged as a const method, and therefore it is acceptable to pass a reference to a's natural type (int *) to the method it calls.
In this method:
void change(int newValue,int *&what) const{
[...]
what=new int(25); //ok but how ?
}
.... changing the value of what is allowed, because what is a non-const reference. The compiler has no way of knowing that what is actually pointing to a member-variable of this object. (Well, in this particular example it could know, but in the general case this change() method could be called from a different translation-unit/file, and in that scenario the compiler would not know anything about what what is aliasing)
So, rather than try to add a rule that would require the compiler to do full-program analysis to enforce consistently, the C++ committee decided to make the const-correctness rules apply on a per-method basis; that way, even if it allows some "const-violations in spirit" like this to slip through the cracks, the rules are more practical to implement and easier to understand.

Initializing an object as in a vector class [closed]

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

c++ classes with parameters and comparisons [closed]

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 9 years ago.
Improve this question
let's say i have a class of type human and i want to create a method function called jim.punch(billy); i created jim and i created billy. how do i refer to jim when i'm writing the function? let's say whatever returns is based on their weight. so if billy is bigger, something will happen and something else will happen if jim is bigger. i just don't know how to use jim in the function
#include <iostream>
using namespace std;
class dog
{
public:
int age;
int weight;
int height;
int punch(int);
};
int jim::punch(x)
{
if (jim > x) <------------------
{
return //something
}
else
{
return something
}
int main()
{
human jim;
jim.age = 30";
jim.weight = 175;
jim.height = 6;
human billy; //etc
jim.punch(billy);
return 0;
}
You should really follow a good book (or at least a good online tutorial); your question shows confusion about very basic C++ concepts.
Nevertheless, here's an answer to your particular situation (omitting loads of details and important-but-not-for-this-particular-case concepts). human is a class. Classes can have data members and member functions. Many instances (or obejcts) of a class type can exist; each of these has its own values of data members.
Each member function can access the members (data and functions) of the instance on which it was invoked. It can do so explicitly (using the keyword this which represents a pointer to the instance), or implicitly (just naming the data member).
So here's how you might express your situation in code:
class human
{
//place data members such as age, height, weight here
public:
int punch(const human &target);
};
int human::punch(const human &target)
{
std::cout << "PUNCH!";
if (weight > target.weight) //equivalent to this->weight > target.weight
{
return /* something which represents this object winning */
}
else
{
return /* something which represents target object winning */
}
//Note: you should handle == case as well
}
int main()
{
human jim, billy;
jim.punch(billy);
}

Why can't my silly cardgame classes talk to each other? [closed]

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.

Defining constructors in a class that uses pointers (C++) [closed]

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 8 years ago.
Improve this question
Okay, so this is my first question on this site, and I'm fairly new to C++. I am trying to create a class of functions that puts the members of various campus clubs into their respective clubs. I was given this class skeleton to follow, but I'm not sure how to define the three constructors, especially how to use the variables that are inside of the parameters. You can pretty much ignore all of the public functions except the constructors. Any ideas?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Club
{
public:
Club();
Club(Club &c);
Club(string cname);
void addMember(string name);
void removeMember(string name);
const string getClubName();
void loadClub();
const bool isMember(string name);
const string getAllMembers();
friend Club mergeClubs(Club& c1, Club& c2);
~Club();
private:
string *members;
int numMembers;
string clubName;
};
You have three constructors: the default Club(), the copy constructor Club(Club &c) and a third one Club(string cname).
For the default, you're going to have to decide what all the default values for your private members are.
For the copy constructor, all you need to do is copy everything from &c into your own record. So for instance you can do:
numMembers = c.numMembers ;
For the third one, you have to decide what cname is and what you should do with it.
For the copy constructor, you do have one tricky decision to make. What does it mean to copy string * members; You might think you could just say members= c.members ; but then if the original Club gets resized, the copy will be pointing to invalid memory.
Instead of using an array of strings, just use a vector, then you don't need the "numMembers" either.
class Club
{
public:
...
void addMember(string name) { members.push_back(name); }
void removeMemeber(string name) { members.erase(std::remove(members.begin(), members.end(), name), members.end()); }
...
private:
vector<string> members;
string clubName;
};