c++

Link error using templates

I converted a function to a template, and started getting this error. I must not be understanding a limitation of templates. Can someone tell me why this is broken? I am receiving this error: Undefined symbols: "bool foo<int>(int const&, int const&)", referenced from: _main in file1.o ld: symbol(s) not found When I link the...

UPDATE: C++ Pointer Snippet

Greetings again, and thanks once more to all of you who provided answers to the first question. The following code is updated to include the two functions per the assignment. To see the original question, click here. I am pretty sure this fulfills the requirements of the assignment, but once again I would greatly appreciate any assist...

Designing a lazy vector: problem with const

I wrote a little "lazy vector" class (or, delayed vector) which is supposed to look like a std::vector and usable wherever a std::vector is used, but it loads its elements "lazily", i.e. it will load element n (and possibly a few more) from disk whenever someone accesses element n. (The reason is that in my app, not all elements fit into...

An odd C++ error: test.cpp:15: error: passing ‘const *’ as ‘this’ argument of ‘*’ discards qualifiers

Hi all, I'm having some trouble with a particular piece of code, if anyone can enlighten me on this matter it would be greatly appreciated, I've isolated the problem down in the following sample: #include <iostream> using namespace std; class testing{ int test(); int test1(const testing& test2); }; int testing::test(){ retur...

Will new return NULL in any case?

I know that according to C++ standard in case the new fails to allocate memory it is supposed to throw std::bad_alloc exception. But I have heard that some compilers such as VC6 (or CRT implementation?) do not adhere to it. Is this true ? I am asking this because checking for NULL after each and every new statement makes code look very u...

Compile error: Undefined symbols: "_main", referenced from: start in crt1.10.5.o

Hi all, I have the following code: #include <iostream> using namespace std; class testing{ int test() const; int test1(const testing& test2); }; int testing::test() const{ return 1; } int testing::test1(const testing& test2){ test2.test(); return 1; } after compilation, it gives me the following error: Undefined sy...

Odd behavior with operator>= overloading

Hello, I'm having a strange behavior with an operator overloading in C++. I have a class, and I need to check if its contents are greater or equal to a long double. I overloaded the >= operator to make this check, my declaration is as follows: bool MyClass::operator>=(long double value) const; I have to say that I also have a cast-to-...

How do I convert from a 32-bit int representing time in usec to a 32-bit int representing time as a binary fraction in secs?

POSIX uses struct timeval to represent time intervals. struct timeval { time_t tv_sec; unsigned tv_usec; }; GHS Integrity represents Time in the following manner, struct Time { time_t Seconds; unsigned Fraction; }; For example, 0.5 sec is represented as 0x80000000 and 0.25sec is represented as 0x40000000. What is...

Is there a better way to print a string with cout up to N characters?

-edit- I am sending binary and not a string. My test is using html pages so in this example i am only using a string but my question is about binary, vectors and debugging with ostream. I make this clears some confusion. I have the following code: cout << string(&v[0]).substr(0, len); Is there a better way to print the string v with ...

Reference to value of STL map element?

Is it OK to pass to function a reference to the value of map element, and to modify it there? foo(string & s) { s = "xyz"; } map<int, string> m; m[1] = "abc"; foo(m[1]); // <-- Is it ok? Will m[1] be "xyz" after this call? Thank you. ...

C++ File Handling

I Googled around, but didn't find any good example / tutorial. So i asking you SOF: How do you Read and Write to a File in C++? ...

C++ fstream error

I learning c++, started learning File Handling today. but getting a error when runinng this code #include <iostream> #include <fstream.h> using namespace std; int main() { fstream file; file.open("test.txt",ios::in|ios::out) file.close(); return 0; } Gets error Cannot open include file...

Testing pointers for validity (C/C++)

Is there any way to determine (programatically, of course) if a given pointer is "valid"? Checking for NULL is easy, but what about things like 0x00001234? When trying to dereference this kind of pointer an exception/crash occurs. A cross-platform method is preferred, but platform-specific (for Windows and Linux) is also ok. Update for...

C++ Read Lines from File

I'm learning File Handling in C++, but there is a problem here. I am trying to read a file. This code is meant to output Hello World. but it outputs 0x22fed8. #include <iostream> #include <fstream> using namespace std; int main() { fstream file; file.open("test.txt",ios::in|ios::out); file << "Hello Wor...

Compiler not creating templated ostream << operator

I have a class, defined in a head as: template <typename T> class MyClass { template <typename U> friend std::ostream& operator<<(std::ostream& output, const MyClass<U>& p); public: ... } In an implementation file, I have: template <typename U> std::ostream& operator<<(std::ostream& output, const MyClass<U>& m) { outpu...

Microsoft Visual Studio (2008) - Filters in the Solution Explorer

In the Solution Explorer when working with C++ projects there is the standard filters of Header Files, Resource Files, and Source Files. What I'm wanting to accomplish is essentially Filters by folder. Lets say the structure of the files was like this: ../Folder1/Source1.cpp ../Folder1/Header1.h ../Folder1/Source2.cpp ../Folder1/Hea...

Can`t really understand what the parameters for constructing tcp::resolver::query

Hi everybody, I am starting Boost.Asio and trying to make examples given on official website work. here`s client code: using boost::asio::ip::tcp; int _tmain(int argc, _TCHAR* argv[]) { try { boost::asio::io_service io_service; tcp::resolver resolver(io_service); tcp::resolver::query query(argv[1], "daytime"); ...

Deploying application with Python or another embedded scripting language

I'm thinking about using Python as an embedded scripting language in a hobby project written in C++. I would not like to depend on separately installed Python distribution. Python documentation seems to be quite clear about general usage, but I couldn't find a clear answer to this. Is it feasible to deploy a Python interpreter + standar...

Symbian OS/C++ Descriptors Port?

Does anyone know if there's a working port of the Symbian OS C++ Descriptors functionality to other operating systems? I recall there being some code towards that here, although last time I tested it, it did not compile with G++ due to some missing/undefined types. Thanks in advance, Tyson ...

Method chaining + inheritance don't play well together?

Consider: // member data omitted for brevity // assume that "setAngle" needs to be implemented separately // in Label and Image, and that Button does need to inherit // Label, rather than, say, contain one (etc) struct Widget { Widget& move(Point newPos) { pos = newPos; return *this; } }; struct Label : Widget { Label& setTex...