How do I assign text to the "void PrintIntro" function? - c++

So far I have typed this,
#include iostream
using namespace std;
void PrintIntro();
I want to assign a actual text now to the PrintIntro function so that in my main program I can just type
PrintIntro() and when the program runs the text assign to the function will show.
So far I have tried this after "void PrintIntro();"
{ /*PrintIntro*/
cout <<
"==================================================" << endl;
cout <<
"Welcome to the Math Practice Program!!!!!" << endl;
cout <<
"This Program will help you practice elementary math" << endl;
cout <<
"==================================================" << endl;
/*PrintIntro*/
}
But then I get the error under the "{" symbol indicating that it is "expecting a declaration." I have been searching through notes and messing with this all day and I cannot figure it out. Any help would be appreciated. I am using MS Visual studio Express 2013.

Adding the semicolon after void PrintIntro() tells the compiler that there is a function called PrintIntro that takes no arguments and returns void, and that you are defining it later. This is called a forward declaration. Chances are this is what's happening:
void PrintIntro();
//Compiler: okay, that was a forward declaration
{
//Compiler: wth is this stuff?
}
You want this to happen:
void PrintIntro();
//Compiler: okay, that was a forward declaration
void PrintIntro()
{
//Compiler: oh, this is the definition for that function you told me about earlier
}
Or you want to do it without the forward declaration:
void PrintIntro() //no ';'
{
//Compiler: declaration and function body all in one part - simple!
}
You should also change #include iostream to #include <iostream>

Remove the ; after you declare you function like:
void PrintIntro()
When declaring a function, you do not need to end the function declaration line with a semicolon.
So your function should look like
void PrintIntro(){
blahblahblah...
}

You need to remove the semi column at the end of the declaration
void PrintIntro();
Must be like this
void PrintIntro(){
}

A couple of errors, most of them in the syntax I think. Note that the line #include iostream should actually be #include <iostream> and the ; is missing after void PrintIntro().
Like so
#include <iostream>
using namespace std;
void PrintIntro(){
cout << "Hello world" << endl;
}

Related

Constructor will run but not function from other file

Sorry new to C++ here and cannot find the answers that I am looking for anywhere. I am trying to run the simplest possible program in C++ using OOP and multiple files. If the Vehicle class has no doSomething() function in it, then the constructor prints out just fine. When I add the function and call car.doSomething() it just gives me errors. I have searched for days and can't find a working answer.
main.cpp
#include <stdio.h>
#include <iostream>
#include "Vehicle.h"
using namespace std;
int main(int argc, char **argv){
Vehicle car;
car.doSomething();
return 0;
}
Vehicle.cpp
#include "Vehicle.h"
Vehicle::Vehicle(){
cout << "do something" << endl;
}
void doSomething(){
cout << "do something else" << endl;
}
Vehicle.h
#pragma once
#include <iostream>
using namespace std;
class Vehicle{
public:
Vehicle();
void doSomething();
};
Like I said, new to C++ and not sure how to fix this. Thanks for any help.
Specs:
Codelite v10.0.0,
Linux Ubuntu 18.04
Error: undefined reference to 'Vehicle::doSomething()'
You didn't need to search for days; you only needed to read the chapter in your C++ book about defining member functions.
It goes like this:
void Vehicle::doSomething()
{
cout << "do something else" << endl;
}
That Vehicle:: is how the computer knows that this doSomething definition is for the class Vehicle (just like you did already with the constructor).
Without that, it's just an ordinary function. It doesn't matter that the file is called Vehicle.cpp; C++ doesn't really care about filenames. You could have all sorts of functions, variables, class definitions etc in that file, regardless of whether it were called Vehicle.cpp or Stuff.cpp or Lightness4Eva.cpp (that's not to say that your naming convention is ungood, though!).
I suppose you have "unresolved" linker error in this case. It means error is not in runtime but in build time. Error message could prompt you that linker can't find Vehicle::doSomething(). This would point you that you didn't actually provide doSomething() function. Read error outputs, it helps to understand what is wrong.
write doSomting() defination with class name as in .cpp file:
void Vehicle::doSomething()
{
cout << "do something else" << endl;
}

Overloading a function defined in a namespace

Why is the following code illegal?
#include <iostream>
using namespace std;
namespace what {
void print(int count) {
cout << count << endl;
}
}
void what::print(const string& str) {
cout << str << endl;
}
int main() {
what::print(1);
what::print("aa");
return 0;
}
The error I get when compiling with clang and -std=c++14 is
error: out-of-line definition of 'print' does not match any declaration in namespace 'what'
I know the fix to the problem but I am wondering why the compiler thinks that I am trying to define the function (print) instead of overload it.
The reason it is not working for you is because the syntax
void what::print(const string& str)
is basically saying
inside the what namespace, define the print function here
If you want to define a function outside of its namespace, you must declare it in the namespace beforehand.
§13.1 of the standard states, "When two or more different declarations are specified for a single name in the same scope, that name is said
to be overloaded."
Overloads of a function must be in the same scope of each other. It is just how the language works.

C++ and Visual Studio error - no suitable conversion function from "std::basic_ostream<char, std::char_traits<char>>" to "int" exists

Visual Studio is going crazy on me recently, and gives me the error in the subject when all I did was a simple cout...
CODE:
// Lang.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main{
cout << "hi";
}
int main{
cout << "hi";
}
Due to the possibility of initialising objects in C++ with the {} syntax, your compiler probably interprets this code as an attempt to create a global int variable called main, initialised with the result of std::ostream::operator<<, a member function which returns a reference to the std::ostream itself.
It's as if you had written:
double some_variable { cout << "hi" }
Or:
double some_variable { cout }
std::ostream is actually std::basic_ostream<char, std::char_traits<char>>. And that type is not compatible with int.
The only thing which is strange is why the ; after "hi" does not immediately cause the compiler to stop trying; but you don't say which compiler version and which options you are using.
In any case, all of those facts finally result in the error message:
no suitable conversion function from “std::basic_ostream<char,
std::char_traits<char>>” to “int” exists
and in:
Also, the semicolon after "hi" is highlighted, and says "expected a }"
Solution: make main a function:
int main() {
cout << "hi";
}

Passing a Vector made of a Class to a function (C++)

I'm running into a lot of problems with this, primarily being that my passed Vector of my Class keeps getting flagged as an undeclared identifier. Any help on solving the problem or explanations to help me figure out what I don't understand would be greatly appreciated.
So here is a simplified version of what I have now:
main.cpp
#include "functions.h"
#include "vehicle.h"
int main()
{
int menuSelection;
vector<Vehicle> inventory;
do
{
cout << "Please make a selection:" << endl << endl;
cout << "1: Display Inventory" << endl;
.......
cout << "8 : Write inventory to file and exit" << endl << endl;
if (menuSelection == 1)
{
if (inventory.empty())
cout << "Database empty" << endl;
else
display(inventory);
.......
} while (menuSelection != 8);
return 0;
}
vehicle.h
#pragma once
#include "functions.h"
class Vehicle
{
private:
string VIN;
int year;
.......
// public:
// string getVIN();
.......
}
functions.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void display(vector<Vehicle>);
.......
void display(vector<Vehicle>& in)
{
Vehicle v;
cout << "VIN: " << v.getVIN(in)
}
.......
I've tried a bunch of different things to get it to work so that's why a lot of stuff may seem like odd syntax (I'm also not very good). My assignment is to have a menu in main.cpp which will create a vector from a class stored in vehicle.h, and then the menu is supposed to call functions which are located in functions.h that will communicate through vehicle.h to a fourth not included vehicle.cpp to work with information from the class.
In functions.h, void display(vector<Vehicle>); does not compile because Vehicle is undeclared at this point.
Also, in functions.h, void display(vector<Vehicle>& in) is a different overload to the previous prototype (the & makes a difference), probably not what you intended. And then you place a function body in functions.h -- this should not be there.
You need to organize your code so that Vehicle class definition appears, and then functions.h includes that.
So vehicle.h should look like:
#pragma once
#include <string>
// do NOT include functions.h
class Vehicle
{
// ...
};
and then functions.h should look like:
#pragma once
#include "vehicle.h"
// do NOT do "using namespace std;" in a header and don't include any unnecessary headers
void display(vector<Vehicle> &in);
and then functions.cpp should #include "functions.h" and contain the function body for display.
undeclared identifier means the compiler sees a name but don't see a declaration of that name (as a variable, class, function, etc.). It happens in your code in a few places, for example:
MyClass is used but not declared (main.cpp)
Vehicle is used but not declared (functions.h)
You use Vehicle.v but v not declared in class Vehicle (in addition, I doubt that this is what you intended - if you use class name Vehicle.v it means accessing a static variable, as opposed to vehicle.v).
It seems you lack some basic background. The most important thing is that you learn to decipher compiler errors, so at least you understand what went wrong.

Is equal() included by default in the global namespace?

This is a question regarding the default global namespace in C++. I have the following code that compiles and runs properly using g++ clang-500.2.79.
#include <string>
#include <iostream>
using std::string;
using std::endl;
using std::cout;
bool is_palindrome(const string& str){
return equal(str.begin(), str.end(), str.rbegin());
}
int main(){
cout << "Hello is a palindrome: " << is_palindrome("Hello") << endl;
cout << "madam is a palindrome: " << is_palindrome("madam") << endl;
return 0;
}
My questions is, why does this code compile properly? I forgot to put #include <algorithm> and using std::equal at the beginning of my file. So the expected behaviour is for the compiler to complain.
The example at http://en.cppreference.com/w/cpp/algorithm/equal confirms that I should be using std::equal.
To investigate this further, I tried to track down exactly which version of the equal() function was being called. Being a relative newbie to C++ I don't know exactly how to do this either. I tried,
cout << "The function is: " << equal << endl;
Which generated a compiler error with some interesting information:
/usr/include/c++/4.2.1/bits/stl_algobase.h:771:5:
note: 'std::equal' declared here
Try as I might, I can't find information about stl_algobase (or more probably, I most likely don't understand what I've found). Is stl_algobase a set of functions that are automatically included in the global namespace?
A further questions is: What is the proper way to track (code or otherwise) down which function is being called when you are dealing with potentially overloaded or template functions in C++?
equal is in the std namespace. What you are seeing is argument dependent lookup (ADL). Because the arguments are in the std, the name lookup for equal considers that namespace too.
Here's a simplified example:
namespace foo
{
struct Bar {};
}
namespace foo
{
void bar(const Bar&) {}
void bar(int) {}
}
int main()
{
foo::Bar b;
foo::bar(b); // OK
bar(b); // ADL, OK
foo::bar(42); // OK
bar(42); // No ADL: error: 'bar' was not declared in this scope
}