[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

cpp class help

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 17
Thread images: 1

File: 1469684814484.jpg (6KB, 347x102px) Image search: [Google]
1469684814484.jpg
6KB, 347x102px
I'm trying to call a function of a class from a global function. The class function (referred to as board) is declared by class
Board{
private:
etc.
public:
Board(int, int)

followed by other functions. I'm able to call Board.board(int, int) from my main() function but when calling it from doStuff(), it gives "no matching function to call to 'Board::Board()'. The line that gives the error is Board b; but Board b(given int, given int) worked in my main() function. How do I call a public function after Board::Board without needing to first call Board::Board(int, int)?
>>
>>185168
Note : I'm calling it from doStuff with Board b;

The work my teacher gave me to help me with the previous error I was stuck on calls board via Board board(given int, given int). However, I don't have ints to give Board::Board.
>>
>>185169
Second note : Board::Board is just Board(). Every other public function in Board is void. They're also created in Board and then constructed afterwards while Board::Board is not
>>
>>185168
board()
is not the same thing as
board(1,2)

Read up on polymorphism, and constructors.

If you can't instantiate a class using a no-argument constructor, that means you haven't provided a no-arguement constructor.
>>
>>185173
How would I go about calling the function Board::otherThing(int, int, int)? The way my teacher had it set up, there was only main() with no second function. I changed my setup to reflect his but got confused on the way. I'm considering changing the variable to just be a function in the class but that would probably be a really stupid way of going about it. otherThing(int, int, int) has its own constructor outside of the class whereas my teacher's code had it all in the class under public:
>>
>>185180
Okay, you really need to:

- post the actual code, like on pastebin or something
- read up on how OO works, because you fundamentally don't understand it.

Head First Java is great at explaining OO from first principles, but obviously it's in Java not C++. No big deal though, because the concepts are the same, just the syntax is different, and it's the concepts you're having trouble with.
>>
>>185185
My bad. We haven't really gone too deep into using classes and shit and I missed the last class.
http://pastebin.com/KRBh1MgL is the code. That code gives a segmentation fault caused by a few different things
>>
>>185190
Okay, go google what a "constructor" does.

Your board class doesn't have any constructors at all, which means you can never make an instance of the class, and means you can't call instance methods on board objects, because there's no way to get a board object.

You need to modify buildboard into a constructor, and then you'd just use "board b = new board(x,y);" to get a board object, and from there you can call b.fillarray, b.displayboard, etc. etc.

None of this will make sense to you yet, because you skipped the lesson on how OO works. So do the reading first, then come back.
>>
>>185199
Will do. Thanks for the information, friend.
>>
>>185200
Here's a version that uses constructors and classes.

In C++, arrays can't be dynamic in two dimensions*, so there's a class that makes a two-dimensional array out of a one-dimensional array, and then there's a board class that uses it to store a board.

http://pastebin.com/MEV6UKhi


* (because they're represented as arrays of arrays, and the outer array can't store an object it doesn't know the size of)
>>
>>185247
Thank you so much for helping me out. I did as the other anon suggested and googled constructors but was stuck on how to call it from the other function. Passing b to playerPick made that part so much easier. I can't thank you enough for helping me out and cleaning my trash code.
>>
>>185247
For state, wouldn't that be a 1 dimensional array being treated as a 2 dimensional array? Whenever it's called, it separates the array based on the x_size input when creating the array. Would it be possible to change it into the previous format as well as the way it's called? The task asks us for dynamic 2 dimensional arrays to be used, the dimensions of which be input by the user.
>>
>>185258
It certainly is possible.

This is one of the great things about OO: "encapsulation".

The code calling the TwoDimensionalArray class has absolutely no need to know how it works under the hood, so long as the class implements the contract for TwoDimensionalArray at the start of the program.

This means you can delete TwoDimensionalArray, and write your own one that uses an array of arrays, and all the rest of the code will still work as if nothing has changed.

The next step after encapsulation is inheritance: we could define a base ("abstract") TwoDimensionalArray that defines what a TwoDimensionalArray does, and then write all manner of classes that "inherit" from it, like a SingleArrayTwoDimensionalArray, or a MySQLTwoDimensionalArray or a SQLiteTwoDimensionalArray, and any code that works with TwoDimensionalArray will work with any of them without modification.


Here's a guide to writing arrays of arrays: http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new .
(http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new/28841507#28841507 in the same thread describes what's going on in >>185247).
>>
>>185265
Going back to the assignment, it doesn't state anywhere that it needs to be a two dimensional array. "variable size of board using dynamic memory allocation". I'll be going with your design as it seems to be faster and faster. If a problem arises due to it, I'll bring up the fact that it's not stated what kind of array we should be using. Thank you so much for the help and the links. I've got the rant about using your style bookmarked and I'll be getting back to reading the rest of it in the morning (it's currently 4 am). If you've got a steam account, I could drop you a game in the morning.
>>
>>185265
Another question to help me understand this a bit better, was it boardstate = new TwoDimensionalArray (x,y); that allowed you to then use -> or was it TwoDimensionalArray * boardstate? If it wasn't TwoDimensionalArray * boardstate, would you be able to explain what that line does or the use of * like that? We weren't taught to use * like that or even in the middle of int and the variable name like you did with int * state. It's usually attached to one or the other to create pointers.
>>
>>185283
>We weren't taught to use * like that or even in the middle of int and the variable name like you did with int * state. It's usually attached to one or the other to create pointers.
C doesn't care where the * goes, so
TwoDimensionalArray * boardstate;
TwoDimensionalArray* boardstate;
and
TwoDimensionalArray *boardstate;
are 100% equivalent. They each create a pointer object.

For C++, you use "->" for pointer ("heap") objects, and "." for non-pointer ("stack") objects#.

>was it boardstate = new TwoDimensionalArray (x,y); that allowed you to then use ->
Yes. In C++, "new" allocates an object on the heap for your object pointer to point to. You're then responsible for clearing it up when you're done with "delete". I left out destructors, because the whole program gets cleared up when it finishes, but really Board should use a destructor to free the TwoDimensionalArray it allocates with "new", and TwoDimensionalArray should use a destructor to free its int[]##.

The reason you'd usually allocate objects on the heap is so you can pass it about without copying it all the time. In this specific case, you *have* to allocate on the heap, because the stack frame is allocated when you enter a function, and when you're entering main() and setting up main()'s stack frame, you don't yet know how big the array is going to be.

Thinking about it, you probably should be making a "new Board" in main(). ATM, Board b is getting passed by value, but the code works anyway because boardstate is a pointer###.


# In other OO languages (Java, Python), there's only pointer-objects, and therefore there's only one way to refer to them.
## http://www.learncpp.com/cpp-tutorial/8-7-destructors/
### IOW, it works because Board is an "immutable" object: once you initialise it using the constructor, you can never change it later. If Board were changed to not be immutable, code that passed Boards by value like main() does could break.
>>
>>185296
Oh awesome I didn't know that about the *. That helps a lot. I seriously appreciate everything you've given me so far, especially you going out of your way to completely correct everything I had and giving me links to stuff that would help me understand things better. I've got this all saved for future reference. Thank you so much dude
Thread posts: 17
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.