473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Erros C2371 and C2512!

Please, could someone explain me why there are errors in the following code?

#include <iostream>
#include <string>
using namespace std;

class Log
{
public:
explicit Log(const string& msg) { cout << msg << endl; }
};

int main()
{
string msg = "Hello, World!";

Log(msg.c_str()); // It Works.
Log(msg); // Errors C2371 and C2512!

return 0;
}

I'm using VC++ .NET 2003.

Thanks in advance.

Nov 17 '05 #1
2 1265
Cleber Marinho wrote:
Please, could someone explain me why there are errors in the following code?

#include <iostream>
#include <string>
using namespace std;

class Log
{
public:
explicit Log(const string& msg) { cout << msg << endl; }
};

int main()
{
string msg = "Hello, World!";

Log(msg.c_str()); // It Works.
Log(msg); // Errors C2371 and C2512!

return 0;
}


Log(msg); doesn't get interpreted as an call to the class constructor
(creating a temporary), but as the definition of a local variable just
as when you'd have written:

Log msg;

since you already have defined msg (as a string), you get the
redefinition error, and beacause you don't have a default constructor
defined you get the c2512.

You'll have to explictly create the temporary by using something like:

Log temp(msg);

hth

--
Ben
http://bschwehn.de
Nov 17 '05 #2
Thanx, Ben!

Now it's clear for me.
But, in my case, I prefer to use:

Log::Log(msg);

Thank you again!
"Ben Schwehn" wrote:
Cleber Marinho wrote:
Please, could someone explain me why there are errors in the following code?

#include <iostream>
#include <string>
using namespace std;

class Log
{
public:
explicit Log(const string& msg) { cout << msg << endl; }
};

int main()
{
string msg = "Hello, World!";

Log(msg.c_str()); // It Works.
Log(msg); // Errors C2371 and C2512!

return 0;
}


Log(msg); doesn't get interpreted as an call to the class constructor
(creating a temporary), but as the definition of a local variable just
as when you'd have written:

Log msg;

since you already have defined msg (as a string), you get the
redefinition error, and beacause you don't have a default constructor
defined you get the c2512.

You'll have to explictly create the temporary by using something like:

Log temp(msg);

hth

--
Ben
http://bschwehn.de

Nov 17 '05 #3

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

Similar topics

0
by: LY | last post by:
Hi I'm in the process of upgrading all our solutions and projects from ..NET 2002 to .NET 2003. Well, of course it did not go smoothly. I am running into a problem that I can't figure out. ...
1
by: Reinhard Abel | last post by:
I have an old C-Program written with Borland 2.0. Now I like to do some changes, but I get lot of syntax Erros with my new computer. When I use the old mashine ( 286 ) it works perfectly but I can...
0
by: Scott Chang | last post by:
Hi all, I tried to use Managed C++ coding of VC++.NET (2002)and OpenGL version 1.2 to draw a hexagon shape of benzene. My OpenGLdrawBenzene.cpp is the following: // This is the main project file...
1
by: Mark Fancy | last post by:
I have a project that I'm trying to get to compile. I need to have the /Tc:wchar_t compile switch in order to use some libraries. I have included the following header files: #include <mapix.h>...
6
by: perspolis | last post by:
Hi I have a question about using exceptions or showing error messages .I say this by an example first way: if(n<0 and w<0) MessageBox.Show("please enter positive") else if (n=0 or w>0)...
0
by: WithPit | last post by:
I have some problems with the instantiating new objects. For example I have the following code //Headerfile (Texture1D.h> #pragma once #using <mscorlib.dll> #include <osg/Texture1D> #include...
4
by: junaidnaseer | last post by:
Hi ! I am facing a problem that I have defined a function which when called in the same file generates an error as follows; " visual c error C2371 redefinition basic types see...
2
by: Mohammad Omer | last post by:
Hi, i am developing an application which uses WAB API's, for doing all this i am using vs2k5. I have wab.h header file included in my project to use WAB api's but after compilation one error...
7
by: Samant.Trupti | last post by:
Hi, I am getting this error. error C2371: 'LineCollection' : redefinition; different basic types I have 'LineCollection' defined in two header files in one project Like "typedef...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.