GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Dec-2009, 12:14
sepharoth213 sepharoth213 is offline
New Member
 
Join Date: Dec 2009
Posts: 2
sepharoth213 is on a distinguished road

Class Function Problems...


Well this is the first thing i've (attempted to) program in c++, a crossword solver that will take the shape of a crossword and all of the words in the crossword and solve it using the length of the words and the intersections between word slots.

Right now, i'm stuck at the part where the program looks at the shape of the crossword and determines where the "word slots" are, eg. 1 down, 3 across, whatever.

I'm attempting this through the creation of the classes crosswordRaw, which hold the coordinates of all of the activated "cells" in an array, coordinate, which holds an x and y coordinate, and wordSlot, which will eventually hold a linked list of the cells the word slot contains. Once i get what i have working, i'll create an intersection class, or a way for the linked lists of the wordSlots to refer to each other.

There are also the functions wordStarterChecker, which checks an activated cell on the crossword and determines if it starts a wordSlot, wordSlotProcessor, which counts the length of a wordSlot and determines the intersections it holds, and two others which i'm not having problems with (yet...).

My problems occur in a couple of places,

wordSlot:
CPP / C++ / C Code:
class wordSlot
{
    private:

        coordinate startCordData();
        coordinate endCordData();
        int orientation;

    public:

        wordSlot(int startXCord, int startYCord, int endXCord, int endYCord)
        {
                int boolFlag=0;
                if (startXCord==endXCord)
                {
                    orientation=0;
                }
                else if (startYCord==endYCord)
                {
                    orientation=1;
                }
                else
                {
                    cout << "ERROR! Invalid wordSlot was attempted to be created starting at (" << startXCord << "," << startYCord << ") and ending at (" << endXCord << "," << startYCord << ")." << endl;
                    boolFlag=1;
                };
                if(!boolFlag)
                {
                    coordinate startCord (startXCord, startYCord);
                    coordinate endCord (endXCord, endYCord);
                    startCordData.setCords(startCord); //error: ‘((wordSlot*)this)->wordSlot::startCordData’ does not have class type
                    endCordData.setCords(endCord); //error: ‘((wordSlot*)this)->wordSlot::endCordData’ does not have class type
                }

        }
        ~wordSlot(){};

        coordinate getStartCord()
        {
            return startCordData; //error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type ‘coordinate’ requested
        };

        coordinate getEndCord()
        {
            return endCordData; //error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type ‘coordinate’ requested
        };

}; //error: multiple types in one declaration
(errors are commented in)

wordSlotProcessorVert:
CPP / C++ / C Code:
wordSlot wordSlotProcessorVert(crosswordRaw crossword, int xCord, int yCord)
{
    int slotLength = 0;
    int& rSlotLength = slotLength;  //warning: unused variable 'rSlotLength'
    int endXCord = 0;
    int endYCord = 0;
    int& rEndXCord = endXCord; //warning: unused variable 'rEndXCord'
    int& rEndYCord = endYCord; //warning: unused variable 'rEndXCord'
    int boolFlag=0;
    int& rBoolFlag=boolFlag; //warning: unused variable 'rBoolFlag'
    for(,rBoolFlag=0,yCord++)
    {
        if(crossword.checkCell(xCord,yCord))
        {
            if(crossword.checkCellLeft(xCord,yCord) or crossword.checkCellRight(xCord,yCord))
            { 
                cout << "Intersection Found!" << endl;
            };
            rSlotLength++;
        }
        else
        {
            rBoolFlag=1;
            rEndXCord = xCord;
            rEndYCord = yCord; 
        };
    };
    wordSlot slot(xCord,yCord,endXCord,endYCord);
    return slot;
};

These warning don't appear for the horizontal version, though it's basically the same code, or maybe the compiler just never checked it.

Here's the coordinate class for reference.
CPP / C++ / C Code:
class coordinate
{
    public:

        coordinate(){};

        coordinate(int xCord, int yCord)
        {
            xData=xCord;
            yData=yCord;
        };

        ~coordinate(){};

        void setY(int yCord)
        {
            yData=yCord;
        };

        void setX(int xCord)
        {
            xData=xCord;
        };

        void setCords(coordinate cord)
        {
            yData=(cord.getYCord());
            xData=(cord.getXCord());
        };


        int getXCord()
        {
            return xData;
        };

        int getYCord()
        {
            return yData;
        };

        void changeCord(int xCord, int yCord)
        {
            if(xCord>=0)
            {
                xData=xCord;
            };
            if(yCord>=0)
            {
                yData=yCord;
            };
        };

    private:
        int xData;
        int yData;

}

There's probably something extremely obvious i'm missing, but i wouldn't know. I'm running Ubuntu Karmic, using Code::Blocks and the default GCC compiler (i think).

I've attached a .txt file with the entire code and the errors/warnings.
Attached Files
File Type: txt Crossword Solver App.txt (11.6 KB, 6 views)
  #2  
Old 12-Dec-2009, 23:36
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 388
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Class Function Problems...


Quote:
Originally Posted by sepharoth213
There's probably something extremely obvious i'm missing, but i wouldn't know.
I don't know about the logic of your program, but the errors you get are only caused by lack of knowledge or attention to C++ syntax.

Here's a rundown of the code you attached. I might miss some things.

CPP / C++ / C Code:
class crosswordRaw
{
    public:

        crosswordRaw()
        {
            for (int y=0; y<30; y++)
                for (int x=0; x<30; x++)
                {
                    deleteSquare(x,y);
                }
        };
First of all your surplus usage of semicolons. Function definitions don't need a semicolon after them. It's not an error and you can put 10 semicolons after each one if you like, but they aren't needed. Same goes for if-else, they don't need semicolons either. What DO need semicolons are class declarations. You have one without it's semicolon in your code. For loops also need semicolons:
CPP / C++ / C Code:
for(,rBoolFlag=0,xCord++)
It feels like you took someone else's code and expanded upon it, because you have a perfectly valid for loop earlier in your program. So take a look there. What you probably meant was this:
CPP / C++ / C Code:
for (; BoolFlag == 0; xCord++)

Next there's this:
CPP / C++ / C Code:
int slotLength = 0;
    int& rSlotLength = slotLength;
    int endXCord = 0;
    int endYCord = 0;
    int& rEndXCord = endXCord;
    int& rEndYCord = endYCord;
    int boolFlag=0;
    int& rBoolFlag=boolFlag
Can you explain why you declare a reference for each variable declared? My guess is ancestry from another language. In any case, it's unneeded and looks silly. It makes no difference whether you do
CPP / C++ / C Code:
slotLength++;
or
CPP / C++ / C Code:
rSlotLength++;
The original variable slotLength is incremented in both cases.


CPP / C++ / C Code:
coordinate startCordData();
coordinate endCordData();
In C++ you cannot initialize class objects in this manner inside a class declaration. In fact, you cannot initialize them at all inside the class declaration. Besides, the compiler will parse the above as function declarations. It's silly that way. You need to do
CPP / C++ / C Code:
coordinate startCordData;
coordinate endCordData;
and in your constructor call their coordinate constructor. The preferred way is to use the initializer list:
CPP / C++ / C Code:
wordSlot(int startXCord, int startYCord, int endXCord, int endYCord)
 : startCordData(), endCordData()
{
    int boolFlag=0;
The reason for this is that in C++, before the constructor of a class runs, all members that are class objects are guaranteed to be constructed. This can lead to needless overhead if you call the constructors again inside the constructor body. If the default-constructed object is fine by you, then you can leave them out of the initializer list.

Lastly, there are some minor typos.

My recommendation would be what is often recommended: Code a little, compile a little, test a little. It is quite obvious you coded a lot and compiled a lot.
  #3  
Old 13-Dec-2009, 17:46
sepharoth213 sepharoth213 is offline
New Member
 
Join Date: Dec 2009
Posts: 2
sepharoth213 is on a distinguished road

Re: Class Function Problems...


Well thanks, I'll try this out. I didn't expand on someone else's code though, i just wrote half of it and got it working, and then put it away for a while and took a second hack at it, and yeah lack of knowledge, first time trying and that's why i posted it up.
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 66 16-Jan-2010 10:53
Compiling C btrieve programs in VS 2005 emanresu C Programming Language 1 16-Nov-2009 03:19
Problem executing nam-1.13 RodolfoAlvizu Computer Software Forum - Linux 20 28-Feb-2009 15:23
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Box Class, need help again :( TransformedBG C++ Forum 7 13-Nov-2006 15:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:00.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.