I'm having some trouble overloading my stream extraction operator in C++ for a hw assignment. I'm not really sure why I am getting these compile errors since I thought I was doing it right... Here is my code:
Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
//friend ostream &operator<<(ostream &output, const Complex &complexObj) const;
// note at bottom regarding friend function
public:
Complex(double = 0.0, double = 0.0); // constructor
Complex operator+(const Complex &) const; // addition
Complex operator-(const Complex &) const; // subtraction
void print() const; // output
private:
double real; // real part
double imaginary; // imaginary part
};
#endif
Complex.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
// Constructor
Complex::Complex(double realPart, double imaginaryPart) : real(realPart), imaginary(imaginaryPart)
{
}
// addition operator
Complex Complex::operator+(const Complex &operand2) const
{
return Complex(real + operand2.real, imaginary + operand2.imaginary);
}
// subtraction operator
Complex Complex::operator-(const Complex &operand2) const
{
return Complex(real - operand2.real, imaginary - operand2.imaginary);
}
// Overload << operator
ostream &Complex::operator<<(ostream &output, const Complex &complexObj) const
{
cout << '(' << complexObj.real << ", " << complexObj.imaginary << ')';
return output; // returning output allows chaining
}
// display a Complex object in the form: (a, b)
void Complex::print() const
{
cout << '(' << real << ", " << imaginary << ')';
}
main.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
int main()
{
Complex x;
Complex y(4.3, 8.2);
Complex z(3.3, 1.1);
cout << "x: ";
x.print();
cout << "\ny: ";
y.print();
cout << "\nz: ";
z.print();
x = y + z;
cout << "\n\nx = y + z: " << endl;
x.print();
cout << " = ";
y.print();
cout << " + ";
z.print();
x = y - z;
cout << "\n\nx = y - z: " << endl;
x.print();
cout << " = ";
y.print();
cout << " - ";
z.print();
cout << endl;
}
Compile erros:
complex.cpp(23) : error C2039: '<<' : is not a member of 'Complex'
complex.h(5) : see declaration of 'Complex'
complex.cpp(24) : error C2270: '<<' : modifiers not allowed on nonmember functions
complex.cpp(25) : error C2248: 'Complex::real' : cannot access private member declared in class 'Complex'
complex.h(13) : see declaration of 'Complex::real'
complex.h(5) : see declaration of 'Complex'
complex.cpp(25) : error C2248: 'Complex::imaginary' : cannot access private member declared in class 'Complex'
complex.h(14) : see declaration of 'Complex::imaginary'
complex.h(5) : see declaration of 'Complex'
Thanks!
Edit:
I wasn't sure about declaring the friend function in the header file or not. When I do, I get these errors:
c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2143: syntax error : missing ';' before '&'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2433: 'ostream' : 'friend' not permitted on data declarations
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2061: syntax error : identifier 'ostream'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2805: binary 'operator <<' has too few parameters
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2872: 'ostream' : ambiguous symbol
1> could be 'c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : int ostream'
1> or 'c:\program files\microsoft visual studio 9.0\vc\include\iosfwd(708) : std::ostream'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2143: syntax error : missing ';' before '&'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2086: 'int ostream' : redefinition
1> c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : see declaration of 'ostream'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2872: 'ostream' : ambiguous symbol
1> could be 'c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : int ostream'
1> or 'c:\program files\microsoft visual studio 9.0\vc\include\iosfwd(708) : std::ostream'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2872: 'ostream' : ambiguous symbol
1> could be 'c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : int ostream'
1> or 'c:\program files\microsoft visual studio 9.0\vc\include\iosfwd(708) : std::ostream'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2065: 'output' : undeclared identifier
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(23) : error C2059: syntax error : 'const'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(24) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.cpp(24) : error C2447: '{' : missing function header (old-style formal list?)
1>Generating Code...
1>Compiling...
1>main.cpp
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2143: syntax error : missing ';' before '&'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2433: 'ostream' : 'friend' not permitted on data declarations
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2061: syntax error : identifier 'ostream'
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\wongj\my documents\visual studio 2008\projects\c3_hw\c3_hw4\complex.h(6) : error C2805: binary 'operator <<' has too few parameters
Your commented-out declaration in the header is almost correct. Since it is a friend, it is not a member and thus cannot be const, so it should be:
friend std::ostream &operator<<(std::ostream &output, const Complex &complexObj);
Note also that you need to qualify ostream as std::ostream, since you should not use using namespace std; in a header file (you really shouldn't need to use it anywhere; it's generally better to just write out std:: when you want to use something from the standard library).
Likewise, in your source file, since it is not a member function, you do not prefix the operator definition with the class name, and it should be:
std::ostream &operator<<(std::ostream &output, const Complex &complexObj)
If you want client code to have access to the operator<< overload, add
ostream & operator<<(ostream &output, const Complex &complexObj) const
to your class header file.
If, on the other hand, you only want client code to call print(), remove the
Complex::
scoping from the operator definition in the implementation file.
Related
error C2661: 'std::tuple::tuple': no overloaded function takes 3 arguments
I have created an abstract class called DataManager and it has a pure virtual funnction close(ClientData b) =0 ;
ifndef _DataManager_HPP_
#define _DataManager_HPP_
#include <string>
#include "../ClientBlockData.hpp"
namespace slssm {
enum COMMAND_TYPE { DB_WRITE = 0, DB_READ, DB_NONE };
class DataManager {
public:
virtual std::vector<std::string> Update(ClientBlockData& blkData,
std::string& iQuery) = 0;
virtual void closeDb(ClientBlockData& blkData) = 0;
virtual std::string buildDbQuery(std::string& iTableName,
std::string& msg,
std::string& oTopic,
COMMAND_TYPE& oCmdType) = 0;
// ~DataManager() {}
};
which as a dervied class SQLiteManager in SQLiteManager.hpp
void closeDb(ClientBlockData& blkData) {
mDb=nullptr;
}
in the main function I use it like this
SQLiteManager* dbmang = new SQLiteManager(blkData.mDb, mTableName, iSSMLoggingFlag, qcc);
// Spawn a new listener thread that responds to queries
mQueryHandlerThreads[blkData.mLocalId] =
std::thread(&SQLiteManager::closeDb, dbmang,
blkData);
The reported error
c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\memory(2539,1): error C2661: 'std::tuple<void (__cdecl slssm::SQLiteManager::* )(slssm::ClientBlockData &),slssm::SQLiteManager *,slssm::ClientBlockData>::tuple': no overloaded function takes 3 arguments
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\thread(49): message : see reference to function template instantiation 'std::unique_ptr<std::tuple<void (__cdecl slssm::SQLiteManager::* )(slssm::ClientBlockData &),slssm::SQLiteManager *,slssm::ClientBlockData>,std::default_delete<_Ty>> std::make_unique<std::tuple<void (__cdecl slssm::SQLiteManager::* )(slssm::ClientBlockData &),slssm::SQLiteManager *,slssm::ClientBlockData>,void(__cdecl slssm::SQLiteManager::* )(slssm::ClientBlockData &),slssm::SQLiteManager*&,slssm::ClientBlockData&,0>(void (__cdecl slssm::SQLiteManager::* &&)(slssm::ClientBlockData &),slssm::SQLiteManager *&,slssm::ClientBlockData &)' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\thread(49): message : with
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\thread(49): message : [
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\thread(49): message : _Ty=std::tuple<void (__cdecl slssm::SQLiteManager::* )(slssm::ClientBlockData &),slssm::SQLiteManager *,slssm::ClientBlockData>
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\thread(49): message : ]
You have to use the reference to blkData
mQueryHandlerThreads[blkData.mLocalId] =
std::thread(&SQLiteManager::closeDb, dbmang,
std::ref(blkData));
Im having a compilation error and i cant figure why.
I have enum declaration on the .h file and the .cpp file suppose to use it inside stringToEnum() function
this is the .cpp file
#include "A.h"
A::A(void)
{
}
A::~A(void)
{
}
values A::stringToEnum (string inputString) {
if (inputString == "string1") return val1;
if (inputString == "string2") return val2;
}
this is the header file
class A
{
public:
A(void);
~A(void);
private:
enum values{
val1,
val2,
val3,
val4
};
values stringToEnum (string inputString);
};
this is the error I get:
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2143: syntax error : missing ';' before 'A::stringToEnum'
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2556: 'int A::stringToEnum(std::string)' : overloaded function differs only by return type from 'A::values A::stringToEnum(std::string)'
1> c:\users\documents\visual studio 2010\projects\A.h(22) : see declaration of 'A::stringToEnum'
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2371: 'A::stringToEnum' : redefinition; different basic types
1> c:\users\documents\visual studio 2010\projects\A.h(22) : see declaration of 'A::stringToEnum'
I will be happy for some guidance.
thanks
Since values is contained in A, you need to qualify the name:
A::values A::stringToEnum (string inputString) {
//...
I was playing around with initializer lists and noticed some inconsistencies. Using an initializer_list as the first argument in a constructor causes a compilation error, but this doesn't happen for functions and member functions, or when other arguments precede the initializer_list.
This code illustrates what I mean:
#include <initializer_list>
using namespace std;
struct A
{
A(initializer_list<int> list){}
A(int a, initializer_list<int> list){}
A(initializer_list<int> list, int b){}
A(int a, initializer_list<int> list, int b){}
};
void B(initializer_list<int> list){}
void B(int a, initializer_list<int> list){}
void B(initializer_list<int> list, int b){}
void B(int a, initializer_list<int> list, int b){}
int main(int argc, char** argv)
{
// Ok
A a = {1};
A b(1, {2});
A c(1, {2}, 3);
B({1});
B(1, {2});
B({1}, 2);
B(1, {2}, 3);
// Error
A d({1});
A e({1}, 2);
return 0;
}
And the build output:
1>------ Build started: Project: Linal (Microsoft Visual C++ Compiler Nov 2012 CTP), Configuration: Release2 x64 ------
1> 'Microsoft Visual C++ Compiler Nov 2012 CTP' is for testing purposes only.
1> main.cpp
1>main.cpp(30): error C2059: syntax error : '{'
1>main.cpp(30): error C2059: syntax error : ')'
1>main.cpp(31): error C2059: syntax error : '{'
1>main.cpp(31): error C2143: syntax error : missing ';' before '}'
1>main.cpp(31): error C2143: syntax error : missing ')' before ';'
1>main.cpp(31): error C2059: syntax error : '}'
1>main.cpp(31): error C2059: syntax error : ')'
1>main.cpp(33): error C2059: syntax error : 'return'
1>main.cpp(34): error C2059: syntax error : '}'
1>main.cpp(34): error C2143: syntax error : missing ';' before '}'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Is this correct behaviour? If so, why does this happen?
Please have a look at the following code,
Stack.h
template <typename T>
class Stack
{
public:
Stack(int number)
{
maxSize = number;
top = -1;
stackData = new T*[maxSize];
}
~Stack()
{
delete [] stackData;
}
int count()
{
}
bool isEmpty()
{
if(top==-1)
{
return true;
}
else
{
return false;
}
}
bool isFull()
{
if(top== (maxSize-1))
{
return true;
}
else
{
return false;
}
}
*T pop()
{
if(!isEmpty())
{
return stackData[top--]; // Remove Item From Stack
}
}
*T peek()
{
T *peekData = &stackData[top];
return peekData;
}
void push(T *pushValue)
{
if(!isFull())
{
stackData[++top] = pushValue;
}
}
private:
int maxSize;
T ** stackData;
int top;
};
Main.cpp
#include <iostream>
#include "Stack.h"
#include <iostream>
using namespace std;
int main()
{
int i = 0;
Stack<double> doubleStack(5);
double doubleValue = 1.1;
cout << "pushing elements into the stack" << endl;
while(i<5)
{
doubleStack.push();
}
system("pause");
return 0;
}
When I run this code, I get the following error.
1>------ Build started: Project: CourseWork2, Configuration: Debug Win32 ------
1> Main.cpp
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C2146: syntax error : missing ';' before identifier 'pop'
1> c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(74) : see reference to class template instantiation 'Stack<T>' being compiled
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(54): warning C4183: 'pop': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C2146: syntax error : missing ';' before identifier 'peek'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(60): warning C4183: 'peek': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C2146: syntax error : missing ';' before identifier 'pop'
1> c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\main.cpp(11) : see reference to class template instantiation 'Stack<T>' being compiled
1> with
1> [
1> T=double
1> ]
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): warning C4183: 'pop': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C2146: syntax error : missing ';' before identifier 'peek'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): warning C4183: 'peek': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\main.cpp(18): error C2660: 'Stack<T>::push' : function does not take 0 arguments
1> with
1> [
1> T=double
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Intellelisense is not identifying any method except isFull(), count() and isEmpty(). I can't code the rest because of this!
Why is this? Please help!
You put * at wrong place in function syntax:
Update:
*T pop()
*T peek()
To:
T* pop()
T* peek()
You put the asterisk in the wrong spot. It should be:
T *pop() {
//implementation
}
and
T *peek() {
//implementation
}
It seems that QtConcurrent works fine with QT containers (QList and QVector), but fails with the STL containers, as opposed to what is claimed in the documentation
Here are the dummy functions I want to use on my containers :
void addOne(int & i)
{
++i;
}
int addOneC(const int & i)
{
return i+1;
}
Examples of what works :
int main( int argc, char** argv )
{
// Qt containers
QList<int> l;
l << 1 << 2 << 4 << 3;
QList<int> l1 = QtConcurrent::blockingMapped(l, addOneC);
QtConcurrent::blockingMap(l1, addOne);
// Standard containers
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(4);
v.push_back(3);
QtConcurrent::blockingMap(v, addOne);
}
What does not work :
int main( int argc, char** argv )
{
// Standard containers
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(4);
v.push_back(3);
vector<int> v1 = QtConcurrent::blockingMapped(v, addOneC);
}
It cause a compilation error with atrociously long and obfuscate template errors.
If anyone knew why, it would really help! Thanks!
The error log :
1>C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(442) : error C2825: '_Alloc': must be a class or namespace when followed by '::'
1> .\main.cpp(187) : see reference to class template instantiation 'std::_Container_base_aux_alloc_real<_Alloc>' being compiled
1> with
1> [
1> _Alloc=int
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(442) : error C2903: 'rebind' : symbol is neither a class template nor a function template
1>C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(442) : error C2039: 'rebind' : is not a member of 'global namespace''
1>C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(442) : error C2143: syntax error : missing ';' before '<'
1>C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(442) : error C2039: 'other' : is not a member of 'global namespace''
1>C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(442) : error C2238: unexpected token(s) preceding ';'
1>.\main.cpp(187) : error C2248: 'std::_Container_base_aux_alloc_real<_Alloc>::~_Container_base_aux_alloc_real' : cannot access protected member declared in class 'std::_Container_base_aux_alloc_real<_Alloc>'
1> with
1> [
1> _Alloc=int
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xutility(435) : see declaration of 'std::_Container_base_aux_alloc_real<_Alloc>::~_Container_base_aux_alloc_real'
1> with
1> [
1> _Alloc=int
1> ]
1>.\main.cpp(187) : error C2440: 'initializing' : cannot convert from 'std::_Container_base_aux_alloc_real<_Alloc>' to 'std::vector<_Ty>'
1> with
1> [
1> _Alloc=int
1> ]
1> and
1> [
1> _Ty=int
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
I think you should explicitly give the type of the container to the blockingMapped.
int main( int argc, char** argv )
{
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(4);
v.push_back(3);
std::vector<int> v1 = QtConcurrent::blockingMapped<std::vector<int> >(v, addOneC);
}
Compile and give me the expected result in the simple example you gave.