0

I have some simple hypothetical static class in C++:

#ifndef __STAT_H_
#define __STAT_H_

class Stat {

 private:
  static vector<int> v;

 public:

  static void add_num(int num);
  static void clear_nums();
  static void get_count();
};

#endif

And the ccp file is so:

#include "Stat.h"

vector<int> v;


void Stat::add_num(int num) {
  v.push_back(num);
}

void Stat::clear_nums() {
  v.clear();
}

int Stat::get_num_count() {
  return v.size();
}

Now when I include in main.cpp file "Stat.h" and try to use some static method:

Stat::add_num(8);

the error during compilation is

undefined reference to 'Stat::add_num(int)'

What can be the problem in this case? Thank you.

EDIT: sorry about addresses vector, it should be v there

6
  • 1
    Most likely one of these (I'm looking at you, didn't link implementation): stackoverflow.com/questions/12573816/…. You also might find this interesting: stackoverflow.com/questions/228783/… Commented Dec 9, 2012 at 8:04
  • You should explain what compilation command you are using, and what compiler you have. Commented Dec 9, 2012 at 8:05
  • command is: g++ main.cpp Commented Dec 9, 2012 at 8:09
  • Note: Your include guard is not valid according to the standard. All names beginning with a leading underscore followed by a capital letter are reserved by the implementation in the global namespace. (And as it is a macro, that is included) Names with double underscores are disallowed in all contexts. But that is unrelated to your question. Commented Dec 9, 2012 at 8:12
  • This is unrelated to the error, but there is no concept of static classes in C++. Commented Dec 9, 2012 at 8:25

3 Answers 3

3

It sounds like you have not included stat.cpp in compilation. So your linker cannot find an implementation for the static methods.

Sign up to request clarification or add additional context in comments.

6 Comments

+1. I would additionally note that the declaration of v is wrong. It declares a vector v, not Stat::v.
I included stat.h into main.cpp
@Sergey: But you didn't tell the compiler about stat.cpp -- which is where Stat::add_num is defined. So the compiler (or rather, in this case the linker) is perfectly okay in saying "I don't know where to find this definition".
if I change stat.h to stat.cpp in include directive, it's the same error
|
1

You need to link Stat.o in g++ command, say:

g++ -c -o Stat.o Stat.cpp
g++ -o Stat main.cpp Stat.o

I guess in your Stat.cpp:

vector<int> v;

should be:

vector<int> Stat::v;

There is no compile error if you define local v in Stat.cpp but I guess you intent to use Stat::v

1 Comment

This is also an error; but it is not the one the user is having a problem with.
1

Here's my take on your program, just for reference sake.

Stat.h

#ifndef STAT_H
#define STAT_H

#include <vector>
using std::vector;

class Stat
{
 public:
  static void add_num(int num);
  static void clear_nums();
  static int get_count();

 private:
  static vector<int> v;
};

#endif

Stat.cpp

#include "Stat.h"

vector<int> Stat::v;

void Stat::add_num(int num) { v.push_back(num); }

void Stat::clear_nums() { v.clear(); }

int Stat::get_count() { return v.size(); }

main.cpp

#include "Stat.h"

int main()
{
  Stat s;
  s.add_num(8);
}

Makefile

CC = g++
OBJS = Stat.o
DEBUG = -g
CFLAGS = -Wall -c $(DEBUG)
LFLAGS = -Wall $(DEBUG)

all: build clean

build: $(OBJS)
        $(CC) main.cpp $(LFLAGS) $(OBJS) -o stat

Stat.o: Stat.h
        $(CC) $(CFLAGS) Stat.cpp

clean:
        -rm -f *.o

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.