Unresolved externals error while calling static function - function

This is driving me absolutely nuts. I've tried everything I've found on other posts here, and nothing works... this should NOT be this complicated.
#include <iostream>
#include <iomanip>
using namespace std;
class SavingsAccount
{
private:
float savingsBalance;
static float annualInterestRate;
public:
static void modifyInterestRate(float interestRate)
{
annualInterestRate = interestRate;
}
};
int main()
{
SavingsAccount::modifyInterestRate(.03);
cout << endl;
system("pause");
return 0;
}
Getting an unresolved externals error.

Related

i was supposed to print 5,4,3,2,1 as per the problem statement,but my c++ code is showing some error

ERROR i'm getting
F:\coding\code blocks\hello.cpp|14|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'void')|
my code
#include <iostream>
using namespace std;
void fun(int n)
{
if (n==0) return;
cout<<n<<endl;
fun(n-1);
}
int main()
{
cout << fun(5)<< endl;
}
Your code has two issues:
fun´s return type is void. Thus, this:
cout << fun(5) << endl;
can´t work as expected. To use the value of a void returning function invokes undefined behavior.
The printing in main of n in general is redundant as you already print it in fun().
Rather do it like:
#include <iostream>
using namespace std;
int fun(int n)
{
if (n == 0)
return;
cout << n << endl;
fun(n - 1);
}
int main(void)
{
fun(5);
return 0;
}
int the main function .
instead of writing
cout<<fun(5)<<endl;
write like this.
fun(5);
Your function fun returns void, also there is no need to use cout twice, use it either in main or in the function body fun. Try this code to get the output "as per your problem statement":
#include <iostream>
using namespace std;
int fun(int n)
{
if (n==0)
return 0;
((n-1)==0)?(cout<<n<<endl):(cout<<n<<",");
fun(n-1);
}
int main()
{
fun(5);
return 0;
}

Can't get std::map to insert from function

I am trying to add items to a map that is a private variable in a class based on if certain parameters are met. When I try to use the insert function for std::map or the [] operator, nothing happens. I don't even get an error. During debugging the code executes like everything is fine but the map stays empty.
I have tried multiple ways to insert to the map including the [] operator and different insert arguments.
class foo {
private:
std::map<std::string, int> map;
public:
void bar();
};
In cpp file:
void foo::bar() {
if(condition)
map.insert(std::make_pair("string", 1));
}
There are no error messages or warnings in the compiler or during debugging.
If the basic STD map usage works, maybe the problem sits in your condition implementation
#include <iostream>
#include <map>
class Foo {
std::map<std::string, int> map;
public:
void bar();
void print();
};
void Foo::bar() {
// if (condition) // weird condition causing failure
map.insert(std::make_pair("string", 1));
}
void Foo::print() {
std::cout << map.size() << std::endl;
std::cout << map.at("string") << std::endl;
}
int main(void) {
Foo foo;
foo.bar();
foo.print();
}

vector not in scope c++

I get an error of 'portfolio' is not in scope for 'function.cpp'. I'm trying to call a function with a "vector of structs" passed in the parameters. But i'm assuming i'm not using porfolio correctly. How do get the size of the vector and change the elements inside of the vector? Thank you!
header.h
#include <iostream>
#include <vector>
struct Stocks {
std::string symbol;
std::string company;
int numberOfShares;
double priceOfShares;
double totalPrice;
int time;
};
void sellStock(std::vector<Stocks>& Portfolios, double*);
main.cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<Stocks>portfolio;
int main() {
sellStock(portfolio);
}
function.cpp
for(int i = 0; i < portfolio.size(); i++) {
porfolio[i].numberOfShares = portfolio[i].numberOfShares + 1;
}

Custom Function not being called

I've been having trouble calling my function in my code.
#include <iostream>
#include <cmath>
#include <cstdlib>
void bunky(int);
int main()
{
using namespace std;
bucky(20);
cin.get();
return 0;
}
void bucky(int x)
{
using namespace std;
cout << "My favorite number is " << x << endl;
}
When I try to compile this it says, "[Error]'bucky' was not declared in this scope" Any and all help would be great.
function declaration is wrong change
void bunky(int);
to
void bucky(int);

call an object's member function in std::algorithms (note: this is a different object, not in containers)

My code:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class myClass {
public:
myClass() {
init();
}
void init() {
_myVector.push_back("Hello");
_myVector.push_back("World");
_myVector.push_back("Bye!");
for_each (_myVector.begin(), _myVector.end(), &myClass::print);
}
void print(string &myStr) {
cout << myStr << "." << endl;
}
private:
vector<string> _myVector;
};
int main() {
myClass myObj;
return 0;
}
If _myVector contained myClass objects or pointers, I could use std::mem_fun_ref or std::mem_fun. Is there any way to do the above? And yes, I do not want myClass::print to be static.
for_each (_myVector.begin(), _myVector.end(), &myClass::print);
to be replaced with
for_each (_myVector.begin(), _myVector.end(), bind1st(mem_fun(&myClass::print), this));