![![hi, im wondring why i get this error here my code:

#include <iostream>
#include "Stuff.cpp"

using namespace std;

int main()
{
    //...
}

//Stuff.cpp

#include <iostream>
#include <string>

using namespace std;

bool Correct_Parent(string x)
{
    int p_Left_Counter  = 0;
    int p_Right_Counter = 0;
    for(unsigned int c = 0; c < x.size(); c++)
    {
        if(x[c] == '(' )
            p_Left_Counter++;
        else
        if(x[c] == ')' )
            p_Right_Counter++;
    }
    if( p_Left_Counter != 0 && p_Right_Counter != 0)
        if(p_Left_Counter == p_Right_Counter)
            return(true);
    return(false);
}

//other stuffs

then when i press f1 on it, its show me vs help then it says must past /force to link command line and it work, my question is: why i get this error and why should i use this /force i wanted just use a file that i put there a function then use it in my main function thak you so much.
(http://prntscr.com/1uhn83)

Recommended Answers

All 3 Replies

The style and syntax is horribly wrong. You should work on this, here:

Q: Why do you need to include the .cpp to this? It's just.. not needed.

Here's an alternative:

Stuff.h

#include <string>

bool Correct_Parent(std::string x)
{
    int p_Left_Counter  = 0;
    int p_Right_Counter = 0;
    for(unsigned int c = 0; c < x.size(); c++)
    {
        if(x[c] == '(' )
            p_Left_Counter++;
        else
        if(x[c] == ')' )
            p_Right_Counter++;
    }
    if( p_Left_Counter != 0 && p_Right_Counter != 0)
        if(p_Left_Counter == p_Right_Counter)
            return(true);
    return(false);
}

You don't need to request the whole of the std namespace: using namespace std when you are only using a string.. This is bad.

Then your main would be:

main.cpp

#include <iostream>
#include "Stuff.h"

using namespace std; // Do you really need this?

int main()
{
    //...
}

This should work. I'm not at a compiler though, so, I haven't checked.

commented: yes sorry about syntax, yes i need using namespace std to cin and cout +0

/force makes the linker create an exe even if you have unresolved references or duplicate references. I suspect that line 2 is your problem, including the .cpp file and it's probably part of the solution. Create a header file like normal and include that.

Thank You Phorse,Ssyx: btw i wanted to use a file that content specific functions ,i think i should put in .h like ssxz said ty u guys.
Yes Ssyxz i create header for defintion and .cpp for implimenttation it work, i dint use this for a while, all time i write template class in just .cpp and it work,but with just few functions abd dint work, mybe class we create an objct and it work but funcions in .cpp like this cuz problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.