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);
    if(!g){
      return i;
    }
  }
  return -1;
}
int actioncmd(char* command){
  int act=in_array(shortcuts,command);
  if(act==-1){
    return -1;
  }
  act=in_array(commands,command);

  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)'

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:
action.cpp
See OP for content.

main.cpp

/* 
 * File:   main.cpp
 * Author: dave
 *
 * Created on 16 August 2014, 12:45
 */
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "action.cpp"
#include "utils.cpp"
const int arraySize = 20;
std::string commands[arraySize] = {
    "quit",
    "line"
};
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:
      cmd=actioncmd(cmdval);
      break;
    case 27 :
      exit(0);
      break;
    default:
      cmdval=appendCharToCharArray(cmdval,key);
  }
  glutPostRedisplay();
}
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
  glutFullScreen();
  glutDisplayFunc(draw);
  glutKeyboardFunc(key);
  glutMainLoop();
  return 0;
}

utils.cpp:

#include <string.h>
char* appendCharToCharArray(char* array, char a)
{
  int len=strlen(array);
  char str[len+2];
  strcpy(str,array);
  str[len]=a;
  str[len+1]='\0';
  return str;
}

Makefile:

MKDIR=mkdir
CP=cp
CCADMIN=CCadmin
build: .build-post

.build-pre:

.build-post: .build-impl

clean: .clean-post

.clean-pre:

.clean-post: .clean-impl


# clobber
clobber: .clobber-post

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

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


# all
all: .all-post

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

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


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

.build-tests-pre:
# 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

.help-pre:
# 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":

http://www.daniweb.com/software-development/cpp/tutorials/466177/understanding-c-from-source-to-binaries

Edited 2 Years Ago by Moschops

Comments
Exactly.

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 2 Years Ago by Moschops

This question has already been answered. Start a new discussion instead.