I'm pretty proficient with Java but am not sure what's going on with pointers, classes, or imports/includes in C++. I originally had a size() function but I was getting error about it conflicting with my 'int size;' variable. Can functions not share the same name as variables? I renamed it to getsize but I'm still not sure why this isn't working or what exactly is going on. Error message at bottom.

Not real sure what #ifndef/#define/#endif are for exactly either. Seems to be a safeguard so that files aren't included twice?

THANKS!

main.cpp

#include <iostream>
#include "LList.h"

using namespace std;

int main() {

 cout << "Hello World" << endl;
 LList* l = new LList();
 cout << l->getsize() << endl;
 return 0;
}

LList.h

#ifndef LLISTH
#define LLISTH

class LList {

 private:

  int size;

 public:

  LList();
  int getsize();

};

#endif

LList.cpp

#ifndef LLISTCPP
#define LLISTCPP

#include "LList.h"

LList::LList() {
 this->size = 0;
}

int LList::getsize() {
 return this->size;
}

#endif

[bletchley@mikey test]$ g++ main.cpp LList.cpp -o -Wall Main

Error:

main.cpp: In function âint main()â:
main.cpp:10: error: request for member âgetsizeâ in âlâ, which is of non-class type âLList ()()â

i would try changing this

LList::LList() {
 this->size = 0;
}

// to
LList::LList()
{
       size = 0;
}

// and

int LList::getsize() {
 return this->size;
}

// to

int LList::getsize()
{
       return size;
}

(.text+0x1d4): multiple definition of `LList::LList()'
/tmp/ccnu19Pg.o:LList.cpp:(.text+0x0): first defined here
Main: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o:(.text+0x0): first defined here
Main:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o:(.rodata+0x0): first defined here
Main: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crti.o:(.fini+0x0): first defined here
Main:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o:(.rodata.cst4+0x0): first defined here
Main: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o:(.data+0x0): first defined here
Main: In function `LList::LList()':
(.text+0x1e2): multiple definition of `LList::LList()'
/tmp/ccnu19Pg.o:LList.cpp:(.text+0xe): first defined here
Main:(.rodata+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/i386-redhat-linux/4.1.2/crtbegin.o:(.rodata+0x0): first defined here
Main: In function `main':
(.text+0x146): multiple definition of `main'
/tmp/ccCfFhGo.o:main.cpp:(.text+0x72): first defined here
/usr/bin/ld: Warning: size of symbol `main' changed from 183 in /tmp/ccCfFhGo.o to 140 in Main
Main: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crti.o:(.init+0x0): first defined here
/usr/lib/gcc/i386-redhat-linux/4.1.2/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
Main:(.dtors+0x4): first defined here
collect2: ld returned 1 exit status

Got it to work by changing the include in main.cpp from "LList.h" to "LList.cpp" and only doing "g++ Main.cpp -o Main".

None of the examples I've looked at have includes to .cpp files, only .h files. Is this bad style?

Got it to work by changing the include in main.cpp from "LList.h" to "LList.cpp" and only doing "g++ Main.cpp -o Main".

In this situation you should have left it as #include "LList.h" and compiled it as g++ LList.cpp Main.cpp -o Main that way LList.cpp gets compiled first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.