Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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