The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

C++, interfaces, robots

DelzhandDelzhand Registered User, Transition Team regular
edited December 2007 in Help / Advice Forum
I'm having a bit of trouble nailing down the syntax for a C++ assignment. We're supposed to write a simulation of a lego mindstorms robot using the NXT++ interface, but I can't figure out where to put what. Here's a sample of what I have:

NXTheader.h
namespace NXT{
namespace Motor{
//! If the motor is deactivated, the tribot will halt immediately if the brake is on, or glide to a stop if off
void brakeOn(int port);
}
}

Tribot.cpp
#using "NXTheader.h"
using namespace NXT;

class Tribot {
private:
bool brake;
};

I've got the requisite constructors, #includes, and #defines (for lWheel and rWheel) and other methods, but what I'm stuck on is where do I define the actual body of BrakeOn(int) so that Tribot implements NXTheader.h as an interface?

Delzhand on

Posts

  • JaninJanin Registered User regular
    edited December 2007
    I think you're a bit confused about the purposes of a namespace. Tribot is a class, and classes cannot "implement" namespaces. If that header is one that has been provided to you, you'll want the source file to look something like this (I use :: syntax only because to use namespaces in the .cpp would add two spurious levels of nesting):
    void
    NXT::Motor::brakeOn (int port)
    {
    // implementation here
    }
    

    Janin on
    [SIGPIC][/SIGPIC]
  • Mr.FragBaitMr.FragBait Registered User regular
    edited December 2007
    What you'll end up doing is writing a subset of the NXT functions. Emulate it as much as possible, so you'll need to have a NXT namespace containing and Motor namespace that has the functions. You need to write those functions with the simulator in mind. I.E. you won't be needing to directly using the NXT code, but make something that works the same so a robot would work in either the simulator or real life. Here's an example of doing the functions in a namespace:
    file:header.hpp
    namespace foo{
        namespace bar{
            int method();
        }
    }
    
    file:header.cpp
    #include "header.hpp"
    int foo::bar::method()
    {
    //simulator related code goes here
    }
    

    You'd probably be better off asking on the WebCT discussion board, even if it can get buried in the other threads and opens yourself to peer ridicule. But seriously, your fellow students probably can help you best. Plus, you probably shouldn't try to get help from random boards, especially if you don't tell Sean.

    Mr.FragBait on
  • DelzhandDelzhand Registered User, Transition Team regular
    edited December 2007
    Yeah, I've got a thread up there now, but I've noticed a real lack of input from the Sean/Jim. PA is my last ditch for advice when Google is totally useless, and I try to limit it to syntactical questions rather than implementation.

    It just seems that no matter where I try to define a method, I get a multiple definition error. It's driving me nuts.

    Edit: Hell, even if I copy and paste your code in an empty project, i get the same thing. Maybe it's a compiler issue.

    Delzhand on
  • JNighthawkJNighthawk Registered User regular
    edited December 2007
    Paste le error?

    JNighthawk on
    Game programmer
Sign In or Register to comment.