Here is my code:

#include <string>
extern std::string commands[];
extern std::string shortcuts[];
int in_array(std::string ar[], std::string txt){
  for(int i=0;i<ar->size();i++){
    int g=ar->compare(txt);
      return i;
  return -1;
int actioncmd(char* command){
  int act=in_array(shortcuts,command);
    return -1;

  return act;

I'm simply trying to find out whether a command given by the user is in an array of commands, and, if it is, I want the index.

This is the error:

multiple definition of `in_array(std::string*, std::string)'

I don't have this defined anywhere else, so I thought perhaps there is an existing function called "in_array". So I renamed my function to "in_array_c". Now the error message is:

multiple definition of `in_array_c(std::string*, std::string)'

2 Years
Discussion Span
Last Post by davecoventry

Can you show us the other files (or rather a minimal version of them that still produces the same error message) in the project and how you build the project?


Cheers, yes:
See OP for content.


 * File:   main.cpp
 * Author: dave
 * Created on 16 August 2014, 12:45
#ifdef __APPLE__
#include <GLUT/glut.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "action.cpp"
#include "utils.cpp"
const int arraySize = 20;
std::string commands[arraySize] = {
std::string shortcuts[arraySize]={"q","l"};
std::string history;
int cmd;
char* cmdval;
void draw()
  // code for rendering here
  glutSwapBuffers();   // swapping image buffer for double buffering
  glutPostRedisplay(); // redrawing. Omit this line if you don't want constant redraw

static void key(unsigned char key, int x, int y)
  switch (key)
    case 13:
    case 32:
    case 27 :
int main(int argc, char** argv)
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // enabling double buffering and RGBA
  glutInitWindowSize(1, 1);
  glutCreateWindow("OpenGL"); // creating the window
  return 0;


#include <string.h>
char* appendCharToCharArray(char* array, char a)
  int len=strlen(array);
  char str[len+2];
  return str;


build: .build-post


.build-post: .build-impl

clean: .clean-post


.clean-post: .clean-impl

# clobber
clobber: .clobber-post

# Add your pre 'clobber' code here...

.clobber-post: .clobber-impl
# Add your post 'clobber' code here...

# all
all: .all-post

# Add your pre 'all' code here...

.all-post: .all-impl
# Add your post 'all' code here...

# build tests
build-tests: .build-tests-post

# Add your pre 'build-tests' code here...

.build-tests-post: .build-tests-impl
# Add your post 'build-tests' code here...

# run tests
test: .test-post

.test-pre: build-tests
# Add your pre 'test' code here...

.test-post: .test-impl
# Add your post 'test' code here...

# help
help: .help-post

# Add your pre 'help' code here...

.help-post: .help-impl
# Add your post 'help' code here...

# include project implementation makefile
include nbproject/Makefile-impl.mk

# include project make variables
include nbproject/Makefile-variables.mk

#include "action.cpp"
This tells the preprocessor to paste the contents of the text file "action.cpp" into this position, just as if you had yourself manually typed it in at that position.

So, you have a definition of the function in action.cpp, and a definition of the function in main.cpp

This is multiple definitions of the same function, in the same program.

Remove #include "action.cpp"

Also remove #include "utils.cpp"

I suspect you don't quite understand what the preprocessor, compiler and linker do. This is hampering you significantly. Read this, and look out for the "One Definition Rule":


Edited by Moschops

Votes + Comments

Thank you; that's helpful.

I guess I've been used to using the "uses" declaration in Pascal and was expecting C to implement the same procedures. And, of course, php uses a similar 'include' or 'require' declaration.


I've removed #include "action.cpp"as you've suggested.

However, this gives me an error:
main.cpp:38:27: error: ‘actioncmd’ was not declared in this scope

What is the correct way to make sure that the included files are read? Clearly using #include "action.cpp" doesn't work.


To use a function in C++, when the compiler gets to the point at which you try to use it, the compiler must know about the function already. Specifically, it must know the function's name, its return type, and its input parameters. This must be done separately for every source file being compiled. Each time the compiler starts on a new source file, anything it learned about a previous source file no longer matters

There are two ways to ensure the compiler knows what it needs to know about the function in order to use it.

The first way is to have the complete definition of the function in that source file, somewhere before you try to use it. This is what you were trying before, and because you were doing it in more than one *.cpp file, you hit the problem that you had the exact same function defined in more than one place.

The second way is to have a declaration of the function in that source file, somewhere before you try to use it. The declaration is the bare minimum the compiler needs to know; the name, the return type, the input parameters.

For the function actioncmd, the declaration is as follows:

int actioncmd(char* command);

So you need to put this in your code, somewhere before you try to call the function.

You can do this by just typing that declaration in. Alternatively, you could create what we commonly refer to as a header file. This is simply a plain text file containing useful things such as function declarations.

So, you could create a file named "action.h", and put in it the declaration of every function that is defined in "action.cpp"

You could then use the #include pre-processor directive to have all those declarations put into your code wherever you like, and the pre-processor will type the contents of the file included file into that spot, just as if you had typed it yourself.

So your "main.cpp" file would have, near the top, #include "action.h", the pre-procesor would open the file "action.h", and copy into "main.cpp" the declaration you wrote in "action.h", and now "main.cpp" has an extra line of code in it; the declaration of the function, near the top, before you try to use the function.

So when the compiler starts on the file "main.cpp" that the preprocessor has inserted the extra lines into (the file after the preprocessor has finished with it is commonly called a "translation unit"), it will see that declaration before you try to use the function, and everyone is happy.

Edited by Moschops

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.