Multiple definition woes

Hi everyone!

I'm working on a program that'll do a class of calculations common in chemistry. For this, there are a variety of information that I need to keep track of (i.e. positions of my atoms, type of calculation etc.).

From my reading, it seems that I have two options available: global class objects or static class objects. I'd like to go the static route as that seems to be a better option.

For starters, I have a class that looks like this (watered down):

read_inp.h:
1
2
3
4
5
6
7
8
9
10
11
12
class Read_Inp{
  public:
    .....
    static string give_se_type(){return se_type};
    .....
  private:
    ....
    static string se_type;
    ....
};

#include "read_inp.cc" 


And in my "read_inp.cc" I have
1
2
3
....
string Read_Inp::se_type;
....


I then have a separate file for main:

1
2
3
4
5
6
7
8
9
int main(int argc, char *argv[]){
  string file;
  Read_Inp test;

  file=argv[1];
  read_input(file);

  cout << "Testing static " << test.give_se_type() << endl;
}


In the above segment, the nonmember function read_input() uses the Read_Inp class to set all the variables in that class. However, since that information would be local to only read_input(), I used static type to make it accessible outside of that class (using main for my test).

However, when I compile, I'm getting:

multiple definition of `Read_Inp::se_type'


Since I'm fairly new to C++ (I really started only a couple monthes ago), I don't know how to resolve this problem, and was wondering if any experts could chime in :)

Thanks!






You don't need to re-declare Read_Inp::se_type in your header file, you want to initialize it (give it an initial value.)
Last edited on
I changed my "read_inp.cc" to:

1
2
3
....
string Read_Inp::se_type="default";
....


And I still get the multiple definition problem. I don't think the issue is that I didn't define it since if I remove the "Read_Inp" class commands from main, I don't get this problem and the program compiles and runs fine.

It seems that it's picking up multiple copies of "Read_Inp::se_type" and that's what causing the problem.
Did you remember to add this stuff?
1
2
3
4
5
6
#ifndef READ_INP_H
#define READ_INP_H

//Class header here

#endif 
Last edited on
Yup-I'm all header guarded-I forgot to put that in when I typed the stuff above :)
global class objects or static class objects
Don't know what that is.

1
2
3
4
5
6
class foo{
  static int bar; //declaration
};

//in foo.cpp
int foo::bar; //definition 
You do need to define static members.

#include "read_inp.cc" You should not include source files, but header files. You need to create a 'project' with your IDE, and add the source files to the project.
Last edited on

Don't know what that is.


Sorry-I'm still getting the lingo down :) Basically what I'd like to be able to do is use my Read_Inp class to read and interpret my input file, then later on in the code be able to put #include "read_inp.h" at the start of my .cc file and then have access to everything that was in my input file. I've had some success in doing "Read_Inp inpt" and then treating inpt as a global object, but this seems clunky. Defining some of my members of my class as static seems to closer to what I want it to do, without trucking about a global variable.

You do need to define static members.

I have both a "read_inp.h" and a "read_inp.cc"-I define my static variable in my read_inp.cc file. This seems to be the convention according to what I've read so far-am I correct in this, or do I need to define it somewhere else?

You should not include source files, but header files. You need to create a 'project' with your IDE, and add the source files to the project.

This point has me confused. My first C++ book said this is what I should do, but my second book (and MUCH better book), doesn't do this. Since I'm doing this on linux with a good ol' fashion Makefile, does this mean I should compile my "read_inp.cc" separately, then link it into my main executable?
Static variable members are shared for all the objects of the same class.
By instance, this could be used to count the number of instances, or for some common property (like the charge of the electron, although a function could work too).
I don't think that you should use them here. Instead make a local object and pass it as needed (by [const] reference).

You are definning the variable correctly. The comment was directed to Mathhead200.

Since I'm doing this on linux with a good ol' fashion Makefile, does this mean I should compile my "read_inp.cc" separately, then link it into my main executable?
Yes.
why you should not include source files http://www.cplusplus.com/forum/beginner/34484/#msg186435


The makefile should do something like
g++ -c read_inp.cc
g++ -c main.cc
g++ main.o read_inp.o -o program
Last edited on
I was not aware that you can define an uninitialized static variable. Wait, brain fart... It's calling the default constructor.
Thanks ne555!

I had some time to make that change to my Makefile, and everything is working smoothly :)
Topic archived. No new replies allowed.