Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

Possible malloc issue, not really sure...

malloc

  • Please log in to reply
3 replies to this topic

#1 MichaelNQ

MichaelNQ

    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 25 November 2009 - 02:24 PM

The following code contains (Node*)malloc(sizeof(struct Node))

My compile points to this as the first error in my function. I am confused why it wouldn't like the malloc. All other errors seem to follow because of this.

The error is as follows:

error: no match for ‘operator=’ in ‘headCurrent = (Node*)malloc(24ul)’


This is the first error on line 48, and I can only assume right now all other errors follow because of this.

Thanks for the help.
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct Node {
    
    char eastVal;
    char southVal;

    Node* east;
    Node* south;
};

void buildManhattanGrid(Node* head, ifstream eastFile, ifstream southFile);

int main() {
    Node* head;

    ifstream eastFile, southFile;
    eastFile.open("eastFile.txt", ifstream::in);
    southFile.open("southFile.txt", ifstream::in);

    buildManhattanGrid(head, eastFile, southFile);
    return 0;
}

void buildManhattanGrid(Node* head, ifstream eastFile, ifstream southFile) {

    string eastStr, southStr;
    Node* headPrev, headCurrent, tailCurrent, temp, tailPrev;
    
    while(!eastFile.eof()) {
        getline(eastFile, eastStr);
        getline(southFile, southStr);

        if(headPrev != NULL) {
            //Create the head of each list at coordinate(0,Y)
            headCurrent = (Node*)malloc(sizeof(struct Node));
            headCurrent->eastVal = eastStr.at(0);
            headCurrent->southVal = southStr.at(0);
            tailCurrent = headCurrent;
            headPrev->south = headCurrent;

            for(int i = 2; i < eastStr.length(); i+2) {
                temp = (Node*)malloc(sizeof(struct Node));
                temp->eastVal = eastStr.at(i);
                temp->southVal = southStr.at(i);
                tailCurrent->east = temp;
                tailCurrent = temp;
                headPrev = headPrev->east;
                headPrev->south = temp;
            }
            headPrev = headCurrent;

        } else {
            headPrev = (Node*)malloc(sizeof(struct Node));
            headPrev->east = eastStr.at(0);
            headPrev->south = southStr.at(0);
            tailPrev = headPrev;

            for(int i = 2; i < eastStr.length(); i+2) {
                temp = (Node*)malloc(sizeof(struct Node));
                temp->eastVal = eastStr.at(i);
                temp->southVal = southStr.at(i);
                tailPrev->east = temp;
                tailPrev = temp;
            }
        }
    }
}


  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 25 November 2009 - 02:26 PM

what is the error message?
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 MichaelNQ

MichaelNQ

    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 25 November 2009 - 02:27 PM

The error is as follows:

error: no match for ‘operator=’ in ‘headCurrent = (Node*)malloc(24ul)’


This is the first error on line 48, and I can only assume right now all other errors follow because of this.
  • 0

#4 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 25 November 2009 - 04:35 PM

You're using C++, why not use the new operator instead of using malloc?

headCurrent = new Node();

As for your error (which means the above code wouldn't work anyway), it's because you declared headCurrent to be of a non-pointer type. Observe this line:
Node* headPrev, headCurrent, tailCurrent, temp, tailPrev;
This means that ONLY the headPrev has been declared as a pointer, all the rest are standard Nodes. To fix this, you have to put asterisks before each name you want to be a pointer, like so:
Node* headPrev, *headCurrent, *tailCurrent, *temp, *tailPrev;
That'll resolve the error. You have another error here:
headPrev->east = eastStr.at(0);
            headPrev->south = southStr.at(0);
at line 65-66. east should be eastVal, and south should be southVal.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?





Also tagged with one or more of these keywords: malloc

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download