-2

Edit:

I have 2 functions in one header file. If I cut and paste these functions into the main file, it works perfectly without any errors. That being said, as of right now, the current code gives me the following errors posted below in the errors section

Thank you for any help!!!

Test1314.cpp

/*
#include<string>
#include<map>
#include<iostream>
*/
#include"myFunctions.h"

using namespace std;

//some unrelated code

int main(){

    
    while (run){
        
        size= sizeof(vars)/sizeof(*vars);
        
        cout<<"\nentering function\n\n";
        
        customIn(vars, size); // calling my function that at this time is supposed to just pair the "variables" to their respective type
        
        cout<<"\nexiting function\n\n";
        
        
        cout<<varToName["a"]; //this was to test to see if i could use my function

        checkclose(1);
        
    }
}

myFunctions.h

#ifndef myFunctions
#define myFunctions

#include<iostream>
#include<string>
#include<map>


bool  run=true;
char check,confirm,ss2=253; 
bool checkclose(int s){

    //code to ask the user if they want to run the program again. Returns 'run', a boolean

}





static map <string, int> varToInt;
static map <string, float> varToFloat;
static map <string, string> varToString;
static map <string, char> varToChar;
static map <string, string> varToName ;

int integers421[256];
float decimals246[256];
string strings2365[256];
char characters2762[256];
int varType[256][2];

void customIn(string vars[][3], int size){

    for(int i=0; i<size; i++){
        
        cout<<"\nsize: "<<size<<"\nIndex: "<<i<<"\n";
        
        
        varToName.insert(pair<string,string>(vars[i][1],vars[i][0]));
        
        if(vars[i][2].compare("int")==0){
            
            varToInt.insert(pair<string,int>(vars[i][1],integers421[i]));
            cout<<"mapped "<<vars[i][0]<<" "<<vars[i][1]<<" "<<vars[i][2]<<" to "<<i<<" in the Integers Array";
        }
        else if(vars[i][2].compare("float")==0){
            
            varToFloat.insert(pair<string,float>(vars[i][1],decimals246[i]));
            cout<<"mapped "<<vars[i][0]<<" "<<vars[i][1]<<" "<<vars[i][2]<<" to "<<i<<" in the Floats Array";
        }
        else if(vars[i][2].compare("string")==0){
            varToString.insert(pair<string,string>(vars[i][1],strings2365[i]));
            cout<<"mapped "<<vars[i][0]<<" "<<vars[i][1]<<" "<<vars[i][2]<<" to "<<i<<" in the Strings Array";
        }
        else if(vars[i][2].compare("char")==0){
            
            varToChar.insert(pair<string,char>(vars[i][1],characters2762[i]));
            cout<<"mapped "<<vars[i][0]<<" "<<vars[i][1]<<" "<<vars[i][2]<<" to "<<i<<" in the Characters Array";
        }
    }
    
}
    
    

#endif

The errors that I am getting with this current code

C:\Users\######\test1314.cpp    In function 'int main()':
45  22 'customIn' was not declared in this scope
50  9 'varToName' was not declared in this scope
Erabior
  • 31
  • 3
  • 1
    Just remember that each source file where you include those header files will also have the function (and variables). That breaks the one definition rule. Please learn how to create projects using multiple source files, how to build them into object files, and then link it all together into a single executable program. And if you're using an IDE that will be handled automatically for you. – Some programmer dude Oct 28 '20 at 22:45
  • I'm not including multiple files. I am currently including only 1 header file with 2 different functions. I am not understanding why my program will only compile when I include the second header file. – Erabior Oct 28 '20 at 22:57
  • And think about what happens if you create a second *source* file which also includes `myFunctions.h`. You might want to learn about the concept of [*translation units*](https://en.wikipedia.org/wiki/Translation_unit_(programming)). – Some programmer dude Oct 28 '20 at 23:00
  • @Someprogrammerdude the symbols ARE in the header file that is being included, that is the problem the OP is trying to solve – Remy Lebeau Oct 28 '20 at 23:00
  • When I use your code as it is, it compiles fine if I add a return value to `customIn` function (I was getting `error C4716: 'customIn': must return a value`). I'm using visual studio 2019 to compile. – Onur Onder Oct 28 '20 at 23:01
  • @OnurOnder why not change the return value to `void` instead? The function is not `return`'ing anything, so the return value should not be `int` – Remy Lebeau Oct 28 '20 at 23:02
  • Perhaps the heder file you include isn't the one you really think you're including? Have you saved it? Is it in the same directory as the source file? If you stop after preprocessing (how to do it depends on the compiler and environment you're using) and look at the result, is it what you expect it to be? – Some programmer dude Oct 28 '20 at 23:03
  • Yes, for return type, it doesn't matter, we can change the return type, but the question is claiming this should not compile, but it compiles after that change. I don't get the same error as the original question asks (maybe as mentioned in the other comment, maybe you are not including the correct file) – Onur Onder Oct 28 '20 at 23:05
  • @Erabior I think it's time you take a step back, and go read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then [edit] your question to include a [mcve]. – Some programmer dude Oct 28 '20 at 23:07
  • i changed the return type of customIn() to void but im still getting an error saying customIn() was not declared in this scope – Erabior Oct 28 '20 at 23:08
  • Marginally related: DevC++ is a tool that sits atop a compiler. You can change the compiler it sits atop. Last time I looked, DevC++ shipped with a mingw port of GCC 4.8 or 4.9. and they (mostly) support C++11. [Follow the instructions here](https://stackoverflow.com/questions/30069830/how-to-install-mingw-w64-and-msys2) and you can install GCC10.2 (Supports C++20 (mostly)). Exactly how you convince DevC++ to use this new compiler, I don't know. How well DevC++ will handle the language's syntax changes, I don't know. – user4581301 Oct 28 '20 at 23:10
  • @Someprogrammerdude Yes I have researched it and I have not found any answers to my problem. If I did I did not understand the solution. I cannot reduce the amount of code because I have no idea what is causing the function to not be defined when it is very clearly defined in the header file. – Erabior Oct 28 '20 at 23:13
  • Regarding Dev-C++, it is rather ancient and is unmaintained. And as mentioned it comes with an equally ancient compiler which only partially supports C++11, but defaults to the older C++03 standard. There are many other free environments that comes with or can use more modern compilers. – Some programmer dude Oct 28 '20 at 23:13
  • You can basically remove *everything* in that header file except `#include `, and then define the function as `void customIn(std::string vars[][3], int size){}`. And the main source file include that file and have an equally simple `main` function (`int main() { customIn(0, 0); }`) If you edit your current files as such (making backups of them first) and you still get the same error, it's guaranteed that you're not including the file you think you're including. And you also have your minimal example. – Some programmer dude Oct 28 '20 at 23:16
  • @Erabior, also, one thing you can try (for debugging) is to copy paste the entire code from header file to your cpp file in the place of `include` line (and remove the include), this should be identical in theory(because `include` just does that for you), and it might give you a better error message. I also recommend what Some programmer dude mentioned in the other comment, to replace the entire function and test. – Onur Onder Oct 28 '20 at 23:18
  • The code doesn't cause this problem. We need more information. It could be caused by your project configuration. – Thomas Sablik Oct 28 '20 at 23:22
  • @OnurOnder I have deleted the second header file. I move all functions to the main file and confirmed that everything worked. I then moved everything exactly as it was to the initial header file (there was 2 but there is now only one) and im still getting the same error. definitely an interesting situation haha – Erabior Oct 28 '20 at 23:34
  • Consider where `using namespace std` is placed. Also [read this post](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – G. Sliepen Oct 28 '20 at 23:44
  • Last thing to try (which I had already asked you about): Search your `C:` drive for the file `myFunctions.h`. How many files are found? Where are their locations? If you have more than one, is one located in the same directory as `C:\Users\######\test1314.cpp`? What is the contents of *that* header file? How does the contents compare to the file you're editing? – Some programmer dude Oct 31 '20 at 01:29

1 Answers1

0

Consider where using namespace std is placed. Also read this post. – G. Sliepen

Moving "myFunctions.h" below using namespace std; fixed my problem

Eugene
  • 6,194
  • 1
  • 20
  • 31
Erabior
  • 31
  • 3
  • [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Nov 01 '20 at 01:48