473,669 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

default object argument

Can i do this:

function(std::s tring = "Hello, world"); ?

If not, is there other way that i can because my compiler (gcc 3.2) is
giving an error;

Thanks,
Anil Mamede

Jul 19 '05 #1
3 3046
On Thu, 04 Sep 2003 11:06:25 +0100, Anil Mamede wrote:
Can i do this:

function(std::s tring = "Hello, world"); ?

If not, is there other way that i can because my compiler (gcc 3.2) is
giving an error;


And what was the errot message then? Did you read it?

function (std::string s = "Hello, world");

hth

--
NPV
"Linux is to Lego as Windows is to Fisher Price." - Doctor J Frink

Jul 19 '05 #2
> Can i do this:

function(std::s tring = "Hello, world"); ?

If not, is there other way that i can because my compiler (gcc 3.2) is
giving an error;

Thanks,
Anil Mamede


It would be helpful if you said what the error was. You may even be
able to figure it out yourself if you read the error.

But, yes you can provide a default argument for function parameters of
type std::string with gcc. The following compiles with gcc
#include <iostream>
#include <string>

void f(std::string s = "test" )
{
std::cout << s << std::endl;
}

int main(int argc, char* argv[])
{
f();
f("BLA");

return 0;
}
when executed, it produces the following

test
BLA
Jul 19 '05 #3

Try this:

function(std::s tring("Hello, World"));

Sumit.


Sorry. Please disregard my earlier post. I had misunderstood your query.

Regards,
Sumit.
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1773
by: Frank Bechmann | last post by:
Eventually most of you will not learn much from this because it's just another event in the 'default argument value gotcha' series, but because it cost me some hours yesterday to spot this 'error' in a famous python tool I thought it might still help other people to save some time. I tried to use some method which was documented to write to 'sys.stdout' per default so that changing 'sys.stdout' to bind to another object should allow to...
18
7823
by: Dan Cernat | last post by:
Hi there, A few threads I had a little chat about default values. I am starting this thread because I want to hear more opinions about the default values of function parameters. Some say they see no use of them. Others say thar they are bad. I like them. So, could anyone tell me why they are in the standard? Are they bad? I do not intend to start war. Nor am I a troll.
5
2300
by: netvaibhav | last post by:
Hi All: Here's a piece of Python code and it's output. The output that Python shows is not as per my expectation. Hope someone can explain to me this behaviour: class MyClass: def __init__(self, myarr=): self.myarr = myarr
44
2753
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
1
11863
by: avasilev | last post by:
Hi all, I am upgrading a kind of small logging framework. Until now, the interface for logging messages takes only a format string, and a variable argument list (in a similiar way as printf()). The interface to the code that uses the logging facilites is through macros, i.e. one ogf the macros looks like this: #define LOG(fmtString, ...) GlobalLogger.LogToFile(fmtString"\n", ##__VA_ARGS__)
50
3300
by: LaundroMat | last post by:
Suppose I have this function: def f(var=1): return var*2 What value do I have to pass to f() if I want it to evaluate var to 1? I know that f() will return 2, but what if I absolutely want to pass a value to f()? "None" doesn't seem to work.. Thanks in advance.
6
1361
by: Igor V. Rafienko | last post by:
Hi, I was wondering whether it was possible to find out which parameter value is being used: the default argument or the user-supplied one. That is: def foo(x, y="bar"): # how to figure out whether the value of y is # the default argument, or user-supplied?
4
3707
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
1
2883
by: Subrat | last post by:
Can we have a class object as default argument to a function? E.g. class A{ } A obj; class B { fun(..., A ObjA/*as default argument*/) }
2
6633
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
8383
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8895
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.