Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 19th, 2006, 9:48 PM   #1
Xyhm
Programmer
 
Xyhm's Avatar
 
Join Date: Mar 2006
Posts: 60
Rep Power: 3 Xyhm is on a distinguished road
Problem with Visual C++ Express

I've just started working on a project, in which part of the code is pre-written. This line generates an error message:

using std::time;

The compiler complains that time isn't a member of std. I don't know whether it is, but that code is supposed to work. As I mentioned in the title, I'm using Visual C++ 2005 Express edition.
Xyhm is offline   Reply With Quote
Old Apr 19th, 2006, 9:55 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Did you include the appropriate header? A 'using' statement doesn't magically crap these thangies, it just announces their birth.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 19th, 2006, 10:01 PM   #3
Xyhm
Programmer
 
Xyhm's Avatar
 
Join Date: Mar 2006
Posts: 60
Rep Power: 3 Xyhm is on a distinguished road
First I had time and stdlib, but VC++ didn't like these so I had to change them to time.h and stdlib.h. Does that explain the error?

Edit: Actually I think is does, and that it has to do with the fact that namespace was new in C++ and *.h is from C. And it seems to work simply by excluding that using-declaration. I'm not planning on duplicating any identifier anyway.

Last edited by Xyhm; Apr 19th, 2006 at 10:12 PM.
Xyhm is offline   Reply With Quote
Old Apr 19th, 2006, 10:28 PM   #4
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
I'm not sure about VC++ but if it follows the C++ standard then you generally would include <ctime> and <cstdlib> header files. By appending a "c" to the C header filename and dropping the .h extension C++ will make available the C functions you're used to. As a note, you might still have to whatever namespace you want to use but I'm hoping someone else can clarify that since I've never done VC++.
aznluvsmc is offline   Reply With Quote
Old Apr 19th, 2006, 10:36 PM   #5
Xyhm
Programmer
 
Xyhm's Avatar
 
Join Date: Mar 2006
Posts: 60
Rep Power: 3 Xyhm is on a distinguished road
Quote:
Originally Posted by aznluvsmc
I'm not sure about VC++ but if it follows the C++ standard then you generally would include <ctime> and <cstdlib> header files. By appending a "c" to the C header filename and dropping the .h extension C++ will make available the C functions you're used to. As a note, you might still have to whatever namespace you want to use but I'm hoping someone else can clarify that since I've never done VC++.
Thanks, "using std::time" works fine with ctime. So we have <ctime> for C++ and <time.h> for C, but what the heck is <time> then, that was written in my code to begin with? Non-standard C++?
Xyhm is offline   Reply With Quote
Old Apr 20th, 2006, 12:34 AM   #6
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
I got another question, on a really basic level. If in VC++, I want to have string objects, and in my program I have:

#include "stdafx.h"
#include "Form1.h"
#include "string.h"  //or <cstring> or <string>, they all give the same errors as below
   .
   .
   .
string str1 = "text";

And I get the following errors:
.\Till Program.cpp(18) : error C2065: 'string' : undeclared identifier
.\Till Program.cpp(18) : error C2146: syntax error : missing ';' before identifier 'str1'
.\Till Program.cpp(18) : error C2065: 'str1' : undeclared identifier

Could somebody tell me what I missed, because in regular C++, that should work.
Twilight is offline   Reply With Quote
Old Apr 20th, 2006, 3:15 AM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
In regular C++ that would not work either, you need to use the std namespace.
Cstring/string.h is not what you want, those are just functions to C strings (i.e. char arrays).
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string str = "bla";
	return 0;
}
If you remove the "using namespace std" line, you will see the same errors you currently have. Alternatively you can use std::string.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 20th, 2006, 5:58 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Again, Visual C++ is a compiler. It works with the ordinary C++ language, the ordinary C++ libraries. Write good code, you're pooty much home free. Compilers are, of course, platform dependent. Some manufacturers include proprietary extensions. Best to steer clear of those. Some are more or less compliant with the standards. VC++ 2005 isn't too bad, in my experience.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 20th, 2006, 5:23 PM   #9
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
Quote:
Originally Posted by nnxion
In regular C++ that would not work either, you need to use the std namespace.
Cstring/string.h is not what you want, those are just functions to C strings (i.e. char arrays).
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string str = "bla";
	return 0;
}
If you remove the "using namespace std" line, you will see the same errors you currently have. Alternatively you can use std::string.
Got it, thanks.
Twilight is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:27 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC