LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-14-2007, 04:40 PM   #1
surfer41
Member
 
Registered: Oct 2005
Distribution: Debian Etch
Posts: 44

Rep: Reputation: 15
c++ and name conflict


If you look at the code below you can't compile this code.

Code:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;

string plus(string x, string y)
{  
   string a(x);
   string b(y);

   string c = a + b;

   return c;
}

int main()
{
   string s1 = "aaa"; 
   string s2 = "bbb";
   string s3 = plus(s1, s2);

   cout << s3 << endl;

   return 0;
}
It gives an error:

Quote:
/usr/include/c++/3.3/bits/stl_function.h: In function `int main()':
/usr/include/c++/3.3/bits/stl_function.h:128: error: `template<class _Tp>
struct std:lus' is not a function,
test2.cc:8: error: conflict with `std::string plus(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::basic_string<char,
std::char_traits<char>, std::allocator<char> >)'
test2.cc:21: error: in call to `plus'
Why am i getting this error? If i use an integer version:

Code:
int plus(int x, int y)
{
   int z = x + y;
   return z;
}
It compiles fine. Why is this strange behavior? Thanks.

Last edited by surfer41; 01-14-2007 at 06:25 PM.
 
Old 01-14-2007, 07:02 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You're re-defining plus, which is already a defined function (from the stl_function.h header). I just changed it from "plus" to "myplus" and it worked just fine.
 
Old 01-14-2007, 07:38 PM   #3
surfer41
Member
 
Registered: Oct 2005
Distribution: Debian Etch
Posts: 44

Original Poster
Rep: Reputation: 15
> You're re-defining plus, which is already a defined function
> (from the stl_function.h header). I just changed it from
> "plus" to "myplus" and it worked just fine.

Thanks for your reply. I understand what you're saying. But one
question though. Original structure plus is defined inside std
namespace and my program is not inside any namespace. I mean
i'm not extending the std namespace. so why can't i use the
plus function which is outside std namespace in that sense?
 
Old 01-14-2007, 07:50 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I'm not 100% sure, but I think this describes the rule which causes that apparent namespace breach.
 
Old 01-14-2007, 08:12 PM   #5
surfer41
Member
 
Registered: Oct 2005
Distribution: Debian Etch
Posts: 44

Original Poster
Rep: Reputation: 15
> I'm not 100% sure, but I think this describes the rule which causes
> that apparent namespace breach.

Yeah that does explain my question. Thanks for help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
IP Conflict mannahazarika Linux - Networking 3 10-21-2006 10:50 AM
Conflict? ltcolfury Linux - Software 2 11-03-2005 08:29 AM
xp conflict with slackware Gunslinger_ROL Slackware 3 09-11-2004 09:29 PM
Nvidia conflict bluedude2288 Linux - Hardware 2 07-19-2004 01:16 PM
libraries conflict jib2 Linux - Software 2 02-03-2004 07:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration