6 Replies - 2085 Views - Last Post: 26 June 2011 - 11:32 PM Rate Topic: -----

#1 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 39
  • View blog
  • Posts: 378
  • Joined: 01-December 08

Converting C++ Code to C#

Posted 26 June 2011 - 07:22 PM

So, I wrote a program in C++ and got it working pretty well, and I'm trying to convert the code to C# without completely rewriting it. So far, I've got the majority of the error messages to go away, but one that I can't figure out how to get rid of is the error "You can only take the address of an unfixed expression inside of a fixed statement initializer." The C++ version uses pointers throughout the program and this error occurs frequently.

An example is this code:
struct node
{
    public node* right;
    public node* left;
    public node* up;
    public node* down;
    public node* colHead;
    public int numNodes;
    public int nodeID;
}


causing this error here in the lines containing (pointer) = &(node):
for (int c = 0; c < 4; c++, currentNode++)
{
    node* current = &nodes[currentNode];
    if (c != 0)
    {
        current->left = &nodes[currentNode-1];
    }
    else
    {
        current->left = &nodes[currentNode+3];
    }
    if (c != 3)
    {
        current->right = &nodes[currentNode+1];
    }
    else
    {
        current->right = &nodes[currentNode-3];
    }
    current->up = headers[values[c]].up;
    headers[values[c]].up->down = current;
    headers[values[c]].up = current;
    current->down = &headers[values[c]];
    current->colHead = &headers[values[c]];
    current->colHead->numNodes++;
    current->nodeID = currentNode;
}


Not sure how I can fix this, any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Converting C++ Code to C#

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,295
  • Joined: 12-June 08

Re: Converting C++ Code to C#

Posted 26 June 2011 - 07:54 PM

Well C# has an issue with unsafe pointers - it's that whole "managed code" business.

Read up on C#'s take on pointers here:
http://msdn.microsof...v=vs.80%29.aspx

For background information here's the help file you can read up on for your specific error:
http://msdn.microsof...v=vs.80%29.aspx
Was This Post Helpful? 0
  • +
  • -

#3 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 39
  • View blog
  • Posts: 378
  • Joined: 01-December 08

Re: Converting C++ Code to C#

Posted 26 June 2011 - 08:08 PM

I'd seen that fix before, but I didn't want to put every single error line in fixed () {} statements. Anyways, I changed one line, headers[i].left = &root; to fixed(headers[i].left = &root) {}, and I got even more errors for just the one line:
Error	1	'solver.headers' is a 'field' but is used like a 'type'
Error	2	The type of a local declared in a fixed statement must be a pointer type
Error	3	You must provide an initializer in a fixed or using statement declaration
Error	4	Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Error	5	The name 'left' does not exist in the current context	
Error	6	You can only take the address of an unfixed expression inside of a fixed statement initializer
Error	7	Only assignment, call, increment, decrement, and new object expressions can be used as a statement


Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Converting C++ Code to C#

Posted 26 June 2011 - 08:49 PM

Based on the small bit of code you posted, you can probably replace every pointer with an object reference. This means you get rid of all the unsafe code, and the '->' become '.'.
Was This Post Helpful? 1
  • +
  • -

#5 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 39
  • View blog
  • Posts: 378
  • Joined: 01-December 08

Re: Converting C++ Code to C#

Posted 26 June 2011 - 09:22 PM

Okay, this got rid of all of the errors and the project compiled, but when I ran it I got
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.


pointing to this line:
    public static void addHeaders()
    {
        for (int i = 0; i < 324; i++)
        {
            if (i == 0)
            {
                headers[i].left = root; // this one
                root.right = headers[i];
            }
            else
            {
                headers[i - 1].right = headers[i];
                headers[i].left = headers[i-1];
            }
            headers[i].up = headers[i];
            headers[i].down = headers[i];
            headers[i].numNodes = 0;
            headers[i].nodeID = i;
        }
        headers[323].right = root;
        root.left = headers[323];
    }


which is the location of the first object reference. I would think this would work because I have this line in the class: public static node root = new node();. Is there something I'm missing?
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Converting C++ Code to C#

Posted 26 June 2011 - 10:36 PM

Probably the creation of the 'headers' objects. Declaring an array doesn't create objects, just a place to hold references to objects.
Was This Post Helpful? 2
  • +
  • -

#7 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 39
  • View blog
  • Posts: 378
  • Joined: 01-December 08

Re: Converting C++ Code to C#

Posted 26 June 2011 - 11:32 PM

Thanks a bunch, Momerath! Works perfectly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1