Related
I have a test where I need to print out std::pair<std::string, std::string> but even though that I declare and define an operator for that googletest will comlain that it cannot find it.
Having google test manual mention that
// It's important that the << operator is defined in the SAME
// namespace that defines Bar. C++'s look-up rules rely on that.
Should I reopen std namespace and place my operator<<(std::ostream& os, const std::pair<std::string, std::string>& p) there?
Excerpt from code:
// included from somewhere else in legacy code:
// typedef ::std::map< ::std::string, ::std::string > AttrList;
bool UserDefinedTypeMatcher::MatchAndExplain(UserDefinedType& r, testing::MatchResultListener* listener) const {
typedef AttrList::const_iterator AttrIt;
std::pair<AttrIt, AttrIt> pattr = std::mismatch(r.attr.begin(), r.attr.end(), expectedUserDefinedType.attr.begin());
if(pattr.first != r.attr.end() && pattr.second != expectedUserDefinedType.attr.end())
{
*listener << "\nFail: attr members differ on the following elements: "
<< "from given UserDefinedType: " << *pattr.first << '\n'
<< ", from expected UserDefinedType : " << *pattr.second << '\n';
return false;
}
return true;
}
Compiler error:
/opt/gmock-1.7.0/include/gmock/gmock-matchers.h: In member function ‘testing::MatchResultListener& testing::MatchResultListener::operator<<(const T&) [with T = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]’:
test/src/matchers/UserDefinedTypeMatchers.cpp:50: instantiated from here
/opt/gmock-1.7.0/include/gmock/gmock-matchers.h:93: error: no match for ‘operator<<’ in ‘*(std::ostream*)((testing::MatchResultListener*)this)->testing::MatchResultListener::stream_ << x’
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:127: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:165: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:169: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:173: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:91: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:180: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:105: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:191: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:200: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:209: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:213: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:221: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:225: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:119: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/gmock-1.7.0/gtest/include/gtest/gtest-message.h:232: note: std::ostream& testing::operator<<(std::ostream&, const testing::Message&)
/opt/gmock-1.7.0/gtest/include/gtest/gtest-test-part.h:120: note: std::ostream& testing::operator<<(std::ostream&, const testing::TestPartResult&)
EDIT:
Ok, so I've learned that I can do the following:
bool UserDefinedTypeMatcher::MatchAndExplain(UserDefinedType& r, testing::MatchResultListener* listener) const {
std::pair<std::string, std::string> mypair;
// use and manipulate mypair
*listener << ::testing::PrintToString(mypair);
}
I'm out of practice with C++ and am writing a "Bank" class for a course I'm taking. I'm getting a long error every time I attempt to compile my Bank.cpp file. I suspect I'm missing something quite obvious here, but I don't know what because the error doesn't make any sense to me.
Here's the cpp file I'm writing that won't compile:
#include <iostream>
#include "Bank.h"
Bank::Bank(): savings(0), checking(0) { }
Bank::Bank(double savings_amount, double checking_amount): savings(savings_amount), checking(checking_amount) { }
void Bank::deposit(double amount, string account)
{
if (account == "S") {
savings = savings + amount;
} else {
checking += amount;
}
}
void Bank::withdraw(double amount, string account)
{
if (account == "S") {
savings -= amount;
} else {
checking -= amount;
}
}
void Bank::transfer(double amount, string account)
{
if (account == "S") {
savings -= amount;
checking += amount;
} else {
checking -= amount;
}
}
void Bank::transfer(double amount, string account)
{
if (account == "S") {
savings -= amount;
checking += amount;
} else {
checking -= amount;
savings += amount;
}
}
void Bank::print_balances()
{
cout << "Savings: $ " << savings;
cout << "Checking: $ " << checking;
}
Here's the header file:
#ifndef BANK_H
#define BANK_H
#include <string>
#include "Account.h"
using namespace std;
class Bank {
private:
Account savings;
Account checking;
public:
Bank();
Bank(double savings_amount, double checking_amount);
void deposit(double amount, string account);
void withdraw(double amount, string account);
void transfer(double amount, string account);
void print_balances();
};
#endif
And here's the "Account.h" header file that is referenced at the top:
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account {
private:
double balance;
double interest_rate;
public:
Account();
Account(double amount, double rate);
void deposit(double);
bool withdraw(double);
double query();
void set_interest_rate(double rate);
double get_interest_rate();
void add_interest();
};
#endif
And here is the cryptic error I'm receiving:
Bank.cpp: In member function ‘void Bank::deposit(double, std::string)’:
Bank.cpp:17: error: no match for ‘operator+’ in ‘((Bank*)this)->Bank::savings + amount’
Bank.cpp:19: error: no match for ‘operator+=’ in ‘((Bank*)this)->Bank::checking += amount’
Bank.cpp: In member function ‘void Bank::withdraw(double, std::string)’:
Bank.cpp:26: error: no match for ‘operator-=’ in ‘((Bank*)this)->Bank::savings -= amount’
Bank.cpp:28: error: no match for ‘operator-=’ in ‘((Bank*)this)->Bank::checking -= amount’
Bank.cpp: In member function ‘void Bank::transfer(double, std::string)’:
Bank.cpp:35: error: no match for ‘operator-=’ in ‘((Bank*)this)->Bank::savings -= amount’
Bank.cpp:36: error: no match for ‘operator+=’ in ‘((Bank*)this)->Bank::checking += amount’
Bank.cpp:38: error: no match for ‘operator-=’ in ‘((Bank*)this)->Bank::checking -= amount’
Bank.cpp:39: error: no match for ‘operator+=’ in ‘((Bank*)this)->Bank::savings += amount’
Bank.cpp: In member function ‘void Bank::print_balances()’:
Bank.cpp:45: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)(& std::cout)), ((const char*)"Savings: $ ")) << ((Bank*)this)->Bank::savings’
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& ()(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& ()(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:127: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& ()(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:165: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:169: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:173: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:91: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:180: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:105: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:191: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:200: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:209: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:213: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:221: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:225: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:119: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>) [with _CharT = char, _Traits = std::char_traits]
Bank.cpp:46: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)(& std::cout)), ((const char)"Checking: $ ")) << ((Bank*)this)->Bank::checking’
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& ()(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& ()(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:127: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& ()(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:165: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:169: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:173: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:91: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:180: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:105: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:191: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:200: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:209: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:213: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:221: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:225: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:119: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>) [with _CharT = char, _Traits = std::char_traits]
[004709613#jb359-5 Lab1]$ Bank.cpp: In member function ‘void Bank::deposit(double, std::string)’:
-bash: syntax error near unexpected token ('
[004709613#jb359-5 Lab1]$ Bank.cpp:17: error: no match for ‘operator+’ in ‘((Bank*)this)->Bank::savings + amount’
-bash: syntax error near unexpected token('
[004709613#jb359-5 Lab1]$ Bank.cpp:19: error: no match for ‘operator+=’ in ‘((Bank)this)->Bank::checking += amount’
-bash: syntax error near unexpected token ('
[004709613#jb359-5 Lab1]$ Bank.cpp: In member function ‘void Bank::withdraw(double, std::string)’:
-bash: syntax error near unexpected token('
[004709613#jb359-5 Lab1]$ Bank.cpp:26: error: no match for ‘operator-=’ in ‘((Bank*)this)->Bank::savings -= amount’
-bash: syntax error near unexpected token ('
[004709613#jb359-5 Lab1]$ Bank.cpp:28: error: no match for ‘operator-=’ in ‘((Bank*)this)->Bank::checking -= amount’
-bash: syntax error near unexpected token('
[004709613#jb359-5 Lab1]$ Bank.cpp: In member function ‘void Bank::transfer(double, std::string)’:
-bash: syntax error near unexpected token ('
[004709613#jb359-5 Lab1]$ Bank.cpp:35: error: no match for ‘operator-=’ in ‘((Bank*)this)->Bank::savings -= amount’
-bash: syntax error near unexpected token('
[004709613#jb359-5 Lab1]$ Bank.cpp:36: error: no match for ‘operator+=’ in ‘((Bank*)this)->Bank::checking += amount’
-bash: syntax error near unexpected token ('
[004709613#jb359-5 Lab1]$ Bank.cpp:38: error: no match for ‘operator-=’ in ‘((Bank*)this)->Bank::checking -= amount’
-bash: syntax error near unexpected token('
[004709613#jb359-5 Lab1]$ Bank.cpp:39: error: no match for ‘operator+=’ in ‘((Bank*)this)->Bank::savings += amount’
-bash: syntax error near unexpected token ('
[004709613#jb359-5 Lab1]$ Bank.cpp: In member function ‘void Bank::print_balances()’:
-bash: syntax error near unexpected token('
[004709613#jb359-5 Lab1]$ Bank.cpp:45: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)(& std::cout)), ((const char*)"Savings: $ ")) << ((Bank*)this)->Bank::savings’
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& ()(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& ()(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:127: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& ()(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:165: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:169: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:173: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:91: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:180: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:105: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:191: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:200: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:209: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:213: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:221: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:225: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:119: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>) [with _CharT = char, _Traits = std::char_traits]
Bank.cpp:46: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)(& std::cout)), ((const char)"Checking: $ ")) << ((Bank*)this)->Bank::checking’
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& ()(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& ()(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:127: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& ()(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:165: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:169: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:173: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:91: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:180: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:105: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:191: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:200: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:209: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:213: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:221: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:225: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void) [with _CharT = char, _Traits = std::char_traits]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:119: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits]
Anyone know what I'm doing wrong? I'm sure it's something pretty stupid. I can post the header file as well, but it's difficult to do so due to remote editing issues.
You need to define the + operator for the Account class so it knows what to do. Since you were trying to add an Account object with a double, you would need this.
double Account::operator+(const double value) const {
return balance + value;
}
If you want to add two account objects, you can overload with this.
Account Account::operator+(const Account &other) const {
return Account(balance + other.balance, interest_rate);
}
If you don't want to add extra operators to your classes, then instead of writing:
savings = savings + amount;
use the already declared methods of the Account class and write:
savings.deposit(amount);
I suspect you declare saving and checking private members as "Account" rather than double. Even if that is your intended design, you can not add user defined class objects without expliclity define operator+(const Account &).
In this line of your source code:
savings = savings + amount;
savings has a type of Account, but amount is a double. Unless you define a + member operator on the Account class like
Account operator+(const double value) const;
Or a global + operator(possible as a friend function) like
Account operator+(const Account& account, const double value);
Then you cannot do such addition.
I had to write a function which binary_print(outstream& outs,unsigned int a)
but when i write a test file for that function it says error.
#include<iostream>
int main()
{
unsigned int d;
std::cout<<"Enter any positive decimal number:";
std::cin>>d;
std::cout<<"Binary of your number is "<<binary_print(cout,d);//<<endl;
std::cout<<'\n';}
}
errors:
testfile_rec.cpp:18:60: error: no match for 'operator<<' in
'std::operator<< [with _Traits =
std::char_traits](((std::basic_ostream&)(& std::cout)),
((const char*)"Binary of your number is ")) <<
binary_print(((std::ostream&)(& std::cout)), d)' ostream:108:7: note:
candidates are: std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ostream<_CharT, Traits>::_ostream_type& (*)(std::basic_ostream<_CharT, Traits>::_ostream_type&)) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:117:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, Traits>::_ios_type& (*)(std::basic_ostream<_CharT, Traits>::_ios_type&)) [with _CharT =
char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream, std::basic_ostream<_CharT, Traits>::_ios_type =
std::basic_ios] ostream:127:7: note:
std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base&
(*)(std::ios_base&)) [with _CharT = char, _Traits =
std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:165:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:169:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:173:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream.tcc:91:5: note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with
_CharT = char, _Traits = std::char_traits] ostream:180:7: note: std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int)
[with _CharT = char, _Traits = std::char_traits,
std::basic_ostream<_CharT, Traits>::_ostream_type =
std::basic_ostream] ostream.tcc:105:5: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits] ostream:191:7: note:
std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with
_CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT, Traits>::_ostream_type =
std::basic_ostream] ostream:200:7: note:
std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with
_CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT, Traits>::_ostream_type =
std::basic_ostream] ostream:204:7: note:
std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned
int) [with _CharT = char, _Traits = std::char_traits,
std::basic_ostream<_CharT, Traits>::_ostream_type =
std::basic_ostream] ostream:209:7: note:
std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT =
char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:213:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:221:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:225:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream.tcc:119:5: note: std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, Traits>::_streambuf_type*) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_streambuf_type = std::basic_streambuf]
If you want to print the binary representation of the given integer, then easy way is to use std::bitset as:
#include<bitset> //include this so as to use std::bitset
unsigned int input;
std::cin >> input;
std::cout << std::bitset<32>(input) << std::endl;
This will print 32-bit binary representation of the value stored in input. It is even better to write this as:
#include <bitset> //for std::bitset
#include <climits> //for CHAR_BIT
std::cout << std::bitset<CHAR_BIT * sizeof(input)>(input) << std::endl;
Now you can wrap this functionality in a function as:
template<typename T>
void binary_print(std::ostream & out, const T & input)
{
out << std::bitset<CHAR_BIT * sizeof(T)>(input) << std::endl;
}
Test code:
int main() {
int input;
std::cin >> input;
binary_print(std::cout, input); //print int-representation
binary_print(std::cout, (short)input); //print short-representation
return 0;
}
Output:
00000000000000000010001000010100
0010001000010100
Online demo : http://ideone.com/OQU6F
cout is an object of class ostream that represents the standard output stream.
Your function incorrecly takes the parameter of type outstream. The function declaration:
binary_p(outstream& outs,unsigned int a)
should be:
binary_p(std::ostream& outs,unsigned int a)
^^^^^^^^
Also, You need to tell your program the namespace in which cout and cin are defined.
Do:
using std::cout;
using std::cin;
In your c++ file.
I am not sure this is the only error because You haven't posted the actual error in the Question.
You probably shouldn't be using it inline with your ostream << calls.
cout << "Binary of your number is ";
binary_print(cout,d);
cout << '\n';
Edit: and indeed the error that you've posted now shows that the compiler can't figure out what operator<< to use with the result of your call to binary_print(). The error basically says "error: no match for 'operator<<' in std::cout<<"Binary of your number is "<<binary_print(cout,d); and then lists all the alternatives it tried to match.
I am writing a small program in C++. There is a class. Can you use cout in the class to output data. Such as cout << "Good times";
Don't you need then to overload << operator?
I am trying to do that but it does not work only some errors. How to output data via cout having it in the class.
CPP file:
#include "University.hpp"
#include "Course.hpp"
//#include "Lecture.hpp"
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
University::University() {
}
University::University(int capacity, string uni) {
university = uni;
cap = capacity;
length = 0;
myCourse = new Course[cap];
}
University::University(University& orig) {
copy(orig);
}
University::~University() {
delete [] myCourse;
}
University & University:: operator=(University & other){
if(this != &other){
delete [] myCourse;
copy(other);
}
return *this;
}
void University:: copy(University & other){
if(this != &other){
length = other.length;
cap = other.cap;
myCourse = new Course[cap];
for(int x = 0; x < length; x++){
myCourse[x] = other.myCourse[x];
}
}
}
void University:: addCourse(Course syllabus){
if(length == cap){
cap *= 2;
Course * temp = new Course[cap];
for(int x = 0; x < length; x++){
temp[x] = myCourse[x];
}
myCourse = temp;
}
myCourse[length] = syllabus;
length++;
}
void University::listAll(){
for(int x = 0; x < length; x++){
cout << toString();// toString();
}
}
friend ostream& operator<<(ostream& os, const University& uni){
os <<uni.university <<uni.myCourse;
return os;
}
string University::toString(){
ostringstream ans;
for(int x = 0; x < length; x++){
ans << "The University " << university << " teaches " << myCourse[x] << myCourse[x].toString() << endl;
}
return ans.str();
}
Header:
#include <string>
#include "Course.hpp"
using namespace std;
#ifndef UNIVERSITY_HPP
#define UNIVERSITY_HPP
class University {
friend ostream& operator<< (ostream& os, const University& uni);
public:
University();
University(int capacity, string UoL);
University(University& orig);
~University();
void copy(University & other);
University & operator=(University & other);
void addCourse(Course syllabus);
void listAll();
string toString();
private:
Course * myCourse;
int length;
int cap;
string university;
};
#endif /* UNIVERSITY_HPP */
errors:
g++ -c -g -MMD -MP -MF build/Debug/Cygwin-Windows/University.o.d -o build/Debug/Cygwin-Windows/University.o University.cpp
University.cpp:72: error: can't initialize friend function `operator<<'
University.cpp:72: error: friend declaration not in class definition
University.cpp: In member function `std::string University::toString()':
University.cpp:81: error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(+std::operator<< [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((std::basic_ostream<char, std::char_traits<char> >&)(+std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)(&ans))), ((const char*)"The University ")))), ((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(((std::string*)((University*)this)) + 12u)))))), ((const char*)" teaches ")) << *(((University*)this)->University::myCourse + (+(((unsigned int)x) * 16u)))'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:63: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:74: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:86: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:121: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:155: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:98: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:178: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:189: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:193: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:179: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:214: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:238: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:219: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:261: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:284: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:307: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
University.cpp:72: note: std::ostream& operator<<(std::ostream&, const University&)
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:504: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:499: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:612: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:567: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:465: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:460: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:505: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:449: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT = char, _Traits = std::char_traits<char>]
make[2]: *** [build/Debug/Cygwin-Windows/University.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory `/cygdrive/g/Aristotelis/C++/Assessment_2'
make[1]: Leaving directory `/cygdrive/g/Aristotelis/C++/Assessment_2'
BUILD FAILED (exit value 2, total time: 3s)
You're trying to call ostringstream::operator<< with the argument being a Course. You need to define operator<< for Course as well. Sorry I missed this before. You should also remove the friend from the definition of operator<<(ostream&, const University&) though since it's only needed inside the class definition.
Try this example.
#include <iostream>
using namespace std;
class A
{
friend ostream& operator<<(ostream& os, const A& a);
int _member;
public:
A() : _member(0)
{ }
A(int member) : _member(member)
{ }
};
ostream& operator<<(ostream&os, const A& a)
{
os << a._member;
return os;
}
int main()
{
A a(5);
cout << a << endl;
return 0;
}
Play with it and fit for your needs.
University.cpp:72: error: can't initialize friend function `operator<<'
University.cpp:72: error: friend declaration not in class definition
University.cpp: In member function `std::string University::toString()':
So, we look In member function `std::string University::toString()' and find:
ans << ... << myCourse[x] << ...;
Are you sure you have the overload for this?
friend ostream& operator<< (ostream& os, const Course& uni);
It's not in any of the code you show here.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Boolean and Void problem
So I have this code that has a sum function that sums n elements in a vector
C++ Syntax (Toggle Plain Text)
#include <iostream>
#include <vector> // need this in order to use vectors in the program
using namespace std;
void computeSum (vector<int> &Vec, int howMany, int total, bool success)
//the computeSum function that will sum positive numbers in a vector
{
int j;
total=0;
for(j=0;j < howMany;j++)
if (Vec[j] > 0){
total+=Vec[j];
} else {
total+=Vec[j+1];
}
if (howMany > Vec[j])
success = false;
else
success = true;
if (total != 0)
success = true;
if (success=true){
cout << total;
} else {
cout << "Oops! Appears you cannot add up these numbers!";
}
}
int main()
{
vector<int> dataVec;
bool success;
int i, n, howMany, total;
cout << "How many numbers would you like to put into the vector? \n";
cin >> n;
dataVec.resize(n);
for(vector<int>::size_type i=0;i < n;i++)
{
cout << "Enter your number for " << i+1 << ": \n";
cin >> dataVec[i];
}
cout << "How many POSITIVE numbers would you like to sum? \n";
cin >> howMany;
cout << "Your total is " << computeSum(dataVec, howMany, total, success);
}
Now before I added the
C++ Syntax (Toggle Plain Text)
bool success;
in the main() - the only error I had was success not declared in main - so now I declared it and I get this...
g++ -I/usr/local/include/bjarne/GUI -I -Wall -O3 -DNDEBUG -Wno-deprecated proj1.cc -o proj1
proj1.cc: In function ‘int main()’:
proj1.cc:59:76: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>]((* & std::cout), ((const char*)"Your total is ")) << computeSum((* & dataVec), howMany, total, ((int)success))’
proj1.cc:59:76: note: candidates are:
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:110:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:110:7: note: no known conversion for argument 1 from ‘void’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&)’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:119:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>, std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:119:7: note: no known conversion for argument 1 from ‘void’ to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&)’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:129:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:129:7: note: no known conversion for argument 1 from ‘void’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:167:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:167:7: note: no known conversion for argument 1 from ‘void’ to ‘long int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:171:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:171:7: note: no known conversion for argument 1 from ‘void’ to ‘long unsigned int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:175:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:175:7: note: no known conversion for argument 1 from ‘void’ to ‘bool’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:93:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:93:5: note: no known conversion for argument 1 from ‘void’ to ‘short int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:182:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:182:7: note: no known conversion for argument 1 from ‘void’ to ‘short unsigned int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:107:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:107:5: note: no known conversion for argument 1 from ‘void’ to ‘int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:193:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:193:7: note: no known conversion for argument 1 from ‘void’ to ‘unsigned int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:202:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:202:7: note: no known conversion for argument 1 from ‘void’ to ‘long long int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:206:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:206:7: note: no known conversion for argument 1 from ‘void’ to ‘long long unsigned int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:211:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:211:7: note: no known conversion for argument 1 from ‘void’ to ‘double’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:215:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:215:7: note: no known conversion for argument 1 from ‘void’ to ‘float’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:223:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:223:7: note: no known conversion for argument 1 from ‘void’ to ‘long double’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:227:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:227:7: note: no known conversion for argument 1 from ‘void’ to ‘const void*’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:121:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:121:5: note: no known conversion for argument 1 from ‘void’ to ‘std::basic_ostream<char>::__streambuf_type*’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/basic_string.h:2694:59: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:451:65: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:456:63: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:462:61: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:468:68: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:473:70: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:493:72: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:323:70: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:510:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:523:75: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:528:77: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
make: *** [proj1] Error 1
....What?
**I even added a return at the end of the int main (( return 0;)) and still am getting the am error
You are trying to cout the return value of computeSum which is void. You can't cout << void.
/* computeSum returns void! */
cout << "Your total is " << computeSum(dataVec, howMany, total, success);
There are quite a few issues with the program as it turns out. I tried to cover everything, but I may have missed some. And because I blabber on and on it's a bit long.
The issue that's giving you the error message is to do with the return type of computeSum(). Right now the return type of computeSum() is void (ie it won't return anything), but in the line where the error occurs you are trying to print whatever is returned by computeSum().
It seems to me like you want 'total' to be returned by computeSum(), not given as a parameter. To do that, instead of making 'total' a parameter declare it at the start of computeSum(), and at the very end of the function add the line
return total;
So computeSum() should look like this:
int computeSum(vector<int> &Vec, int howMany, bool success)
{
int total = 0;
//...
return total;
}
There is also a logic error with the adding up part - think what happens when there are two negative numbers in a row for example.
Here is a better version:
int total = 0;
int i = 0; //the index of the current item being looked at
int numAddedUp = 0; //the number of positive numbers seen
//loop while need to add up more numbers AND i is within the bounds of Vect
//so stop upon reaching end of Vect even if not added up 'howMany' items yet.
while (numAdded < howMany && i < Vect.size())
{
if (Vect[i] > 0)
{
//if it's positive, add it
total += Vect[i];
numAddedUp++;
}
i++;
}
The logic of 'success' seems flimsy to me. If you use the while loop I suggested, you can use this instead:
if (numAddedUp == howMany)
{
success = true;
}
else
{
success = false;
}
or more briefly,
success = (numAddedUp == howMany);
You could extend the check for detecting special cases such as when howMany is less than one. The following line checks that howMany is valid AND the number of items added up matches howMany:
success = (howMany >= 1) && (numAddedUp == howMany);
Also watch out for the line
if (success=true){
which is assigning (=) the value true to 'success', but what you want to do is check for equality (==).
Finally, think about the way you are printing the output. Right now both main() and computeSum() are printing stuff. Ideally, computeSum() wouldn't print anything and the total would be returned along with a bool indicating success/failure. However, a much easier (but not good practice) approach would be to have computeSum() print the output and not even bother returning anything. The main() method would simply have to call the function like this:
computeSum(dataVec, howMany);
as the output will be printed in computeSum() along with nice, detailed, user-friendly error messages such as "The program is not working. Live with it." or "You broke the program!".