Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

C++...help!


  • Please log in to reply

#1
cainiskey

cainiskey

    New Member

  • Member
  • Pip
  • 7 posts
Hi,

Complete amateur question I'm sure, my apologies, but any help would be greatly appreciated and will likely save my hair line.

I have a header file called "endian.h"

Within this I have:
-------------------
#include <iostream>

struct Endian
{
static bool machine_is_little_endian(); //which returns true iff computer is little endian
...
}
-------------------

I have a file called "endian.c"

--------------------
#include <iostream>
#include <endian.h>
using namespace std;

static bool machine_is_little_endian(){
return 0; //because it's big endian
}

----------------------

and a file called "test.c"

----------------------
#include <iostream>
#include <endian.h>
using namespace std;

int main(){
cout << Endian::machine_is_little_endian() << endl;
return 0;
}
-----------------------
And then I go to compile it (g++) and I get "undefined reference to Endian::machine_is_little_endian()"

...I'm losing my mind...
I can get other member functions in Endian working just fine, I declare an Endian variable "x" and then go "x.printZero()" (assuming that printZero is declared and implemented in endian.h and .c). I can't figure out how to get static functions to work though!

If anyone could help me out...just to get test.c printing the return value from the static function would be super!
Thanks very much

~G
  • 0

Advertisements


#2
StephenL

StephenL

    Member

  • Member
  • PipPip
  • 12 posts
hi,
Try moving the implementation of the static function into the .h file and remove it from the .c file. Static functions are always implemented in the .h (something to do with file scope, but I'm sure someone will either correct me or clear the issue for you before long). I tried it just to make sure :

Endian.h

#ifndef __TEST2__
#define __TEST2__

#include <iostream>

struct Endian
{
static bool machine_is_little_endian()//; //which returns true iff computer is little endian
{
return true;
}

..........
other function headers etc..
..........
};

#endif __TEST2__

The way you have called it in the .c file is also correct although I tested in Visual Studio 7

Hope this helps,
Steve
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP