Xcode linker command failed with exit code 1 c++ - c++

I've written a simple series of functions. When I try to call the last function I get the "linker command" error. The syntax is correct but my program won't compile. Am I missing something or is this an IDE issue?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>
using namespace std;
// Function Prototypes
int numGen ();
int questSol ();
int questAns ();
int main() {
// Store values of functions in variables
int ans = questAns();
int sol = questSol();
if (ans == sol){
cout << "Very good! Press Y to continue" << endl;
questAns();
} else {
cout << "Incorrect. Please try again" << endl;
cin >> ans;
if(ans == sol){
questAns();
}
}
return 0;
};
//Generates two random numbers between zero and ten and returns those numbers
int numGen () {
srand(time(0));
int one = rand() % 10;
int two = rand() % 10;
return one;
return two;
};
//Takes in the random numbers, multiplies them, and returns that result
int questSol (int one, int two) {
int solution = one * two;
return solution;
}
//Takes in random numbers, displays them in cout statement as question, receives and returns user answer to
//question
int questAns (int one, int two) {
int answer;
cout << "How much is " << one << " times " << two << "? \n";
cin >> answer;
return answer;
}

You forward declare a function:
int questAns ();
And then later define a function with a signature:
int questAns (int one, int two);
In C++, functions can have the same name but have different parameters (overloaded functions), so you've never actually defined the questAns that you forward declare and then try to call.
Note: You have the same problem with questSol.
It looks like you don't quite understand the scope of local variables.
Inside numGen you define two ints, one and two. Variables defined within a block (curly braces: {}) exist only within that block. They are local to it. The identifier is only valid within the inner-most block it's defined in, and once you exit it that memory is freed. Returning two ints like you're trying is also impossible.
It looks like you're expecting those ints to be available to your other two functions.
The smallest change you could make is to make int one and two global variables. This means you define them outside of any block (usually at the very top of your code). Then remove the parameter lists from your function definitions, because all the functions can see the global variables. That's generally considered bad programming practice because in more complex programs globals wreak havoc on your code, but in this simple program it'd work and give you a chance to practice understanding variable scope.
Another solution, more in line with what you were trying, is to define an ARRAY of two ints, and return that. Then pass that array to the other two functions. That'd be a better way to do it, and give you a chance to learn about arrays.

You have several problems:
numGen - You cannot return two separate values this way
// Function Prototypes
int numGen ();
int questSol ();
int questAns ();
Says that you have 3 functions all of which return an int and are called with no parameters - which is how you call them.
So the linker is looking for functions with a fingerprint of int_questSol_void and int_questAns_void - you then declare two functions that return an int and take as inputs 3 ints - these have fingerprints of int_questAns_int_int and int_questSol_int_int.
As a result the linker is moaning that you are calling to functions that it can't find.

Related

Request for member 'modu18' in f, which is of non-class type 'int'

just started off with c++, and stackoverflow for that matter.
any and help infinitely appreciated, apologies in advance if i ask something super dumb
I'm making a program to solve a problem. How many 8 digit numbers are divisible by 18 and are only comprised of the digits 1, 2 and 3. I can generate numbers, but when calling a function i made to use modulus to determine whether they're divisible by 18 or not, the function gives me the error in the title. my code is as follows:
main.cpp
#include <iostream>
#include "functions.h"
using namespace std;
int main()
{
functions f();
for(int a = 1; a<4; a++){
for(int b = 1; b<4; b++){
for(int c = 1; c<4; c++){
for(int d = 1; d<4; d++){
for(int e = 1; e<4; e++){
for(int f = 1; f<4; f++){
for(int g = 1; g<4; g++){
for(int h = 1; h<4; h++){
int number = 10000000*a + 1000000*b + 100000*c + 10000*d + 1000*e + 100*f + 10*g + h; //generates 8 digit numbers, only using the digits 1 2 and 3
cout << number << endl; //prints numbers
int y = 10; //to test this bloody function
cout << f.modu18()(y); //returns 0 or 1 from func
}}}}}}}}}
functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <iostream>
using namespace std;
class functions
{
public:
functions();
string modu18(int x);
protected:
private:
};
#endif
functions.cpp
#include "functions.h"
#include <iostream>
using namespace std;
functions::functions(){
cout << "Functions initialised!" << endl; //to see if this thing is actually loading
}
string functions::modu18(int x){ //this is checking whether the number is divisible by 18
if(x % 18 == 0){
return 0;
}else{
return 1;
};
} //btw, using multiple files becuase i want to learn how to make this work for the future
The exact error returned when compiling is
request for member 'modu18' in 'f', which is of non-class type 'int'
I have no clue why this is saying this, all my data types are correct, for the data and the function types.
send help pls
Many thanks.
In your function main there are two different f:
First one here
functions f();
is a variable of type functions or at least that is what you expect (see comments, the correct definition would be functions f;. As it is now, this would be the declaration of a function taking no arguments and returning an object of type functions).
The second one here:
for(int f = 1; f<4; f++){
is of type int.
The for loop is in a different scope than the where the first declaration is (each {} introduces a new scope) and therefore it is allowed to reuse identifiers for different objects.
If you refer to the doubly-used name, it will be looked up by certain rules and in the most common case the one in the most-inner scope will be used. Therefore here
cout << f.modu18()(y); //returns 0 or 1 from func
f is the loop counter of type int, not the f declare in the outer scope.
Use different variable names to solve this issue.
Additional stuff unrelated to error message:
Your function modu18 is supposed to return a string, but all your return statements return integer literals which are of integer type and can not be automatically cast to std::string, or rather the implicit conversion will not do what you expect it to.
You do not need classes to use multiple files. In a header file you can declare a free function with string modu18(int); and then define it in the implementation file with string modu18(int a) { ... }.
Using using namespace std; is not good practice, as it will lead to very hard to understand errors down the line. Especially in header files it should never be used. Instead specify all names from the standard library fully with std::.

Prototyping in C++

If I prototype a function above the main function in my code, do I have to include all parameters which have to be given? Is there a way how I can just prototype only the function, to save time, space and memory?
Here is the code where I came up with this question:
#include <iostream>
using namespace std;
int allesinsekunden(int, int, int);
int main(){
int stunden, minuten, sekunden;
cout << "Stunden? \n";
cin >> stunden;
cout << "Minuten? \n";
cin >> minuten;
cout << "Sekunden= \n";
cin >> sekunden;
cout << "Alles in Sekunden= " << allesinsekunden(stunden, minuten, sekunden) << endl;
}
int allesinsekunden (int h, int m, int s) {
int sec;
sec=h*3600 + m*60 + s;
return sec;
}
"If I prototype a function above the main function in my code, do I have to include all parameters which have to be given?"
Yes, otherwise the compiler doesn't know how your function is allowed to be called.
Functions can be overloaded in c++, which means functions with the same name may have different number and type of parameters. Such the name alone isn't distinct enough.
"Is there a way how I can just prototype only the function, to save time, space and memory?"
No. Why do you think it would save any memory?
No, because it would add ambiguity. In C++ it's perfectly possible to have two completely different functions which differ only in the number and/or type of input arguments. (Of course, in a well-written program what these functions do should be related.) So you could have
int allesinsekunden(int, int, int)
{
//...
}
and
int allesinsekunden(int, int)
{
//...
}
If you tried to 'prototype' (declare) one of these with
int allesinsekunden;
how would the compiler know which function was being declared? Specifically how would it be able to find the right definition for use in main?
You have to declare the full signature of your function, i.e. the name, the return value, all parameters with types, their constness, etc.

C++ Defining Global Variable

Very simple question:
I was fiddling with basic C++ (being very new to programming) and I got into trouble while declaring a global variable to do some addition
#include <iostream>
int x,y;
int sum(int, int)
{
return x + y;
}
int main()
{
using namespace std;
cout << "The sum of 10 and 4 is: " << sum(10,4) << endl;
return 0;
}
Changing "int x,y;" to "int x,y = 0" has the same result: The sum equates to 0.
Could someone explain this odd behavior? Thanks!
Your function always returns the sum of global variables x and y, which are always 0. x and y are implicitly set to zero at the program startup. You never change their values, so they remain zero forever. The sum of two zeros is zero, no surprise here.
You pass 10 and 4 to your function, but the function itself completely ignores what is passed to it, i.e. it ignores its parameters (they are not even named). It always sums global x and y, which are always 0.
If you want your function to sum its arguments, you have to name the function parameters and use them
int sum(int a, int b)
{
return a + b;
}
And now you don't need any global variables at all. (main remains as is.)
Alternatively, if you so desire, you can get rid of the parameters completely and sum the global variables instead
int x,y;
int sum()
{
return x + y;
}
but in this case you will have to pass the values to sum through those global variables, not as function arguments
int main()
{
using namespace std;
x = 10;
y = 4;
cout << "The sum of 10 and 4 is: " << sum() << endl;
return 0;
}
This latter approach is here just for illustrative purposes. It is definitely not a good programming practice.
What you have in your code is a weird disconnected hybrid of these two approaches, which can't possibly work.
In order to fix the issue, the thing requires changing is the sum function.
int sum(int a, int b){
return a+b; //a,b here are referring to the inputs, while what you did was referring to the global variable..
}
Besides, try not to use global variables, usually you would end up with lots of troubles.
Another thing, I don't think your way of defining a function is correct. The inputs have to look like this instead:
int sum(int a, int b)
Unless you wanna declare the function first and provide the actual implementation later, you are not suppose to miss the name of the inputs!
when you are just globally declare the variables x,y ,they implicitly set to zero value.in your function definition,you are just giving the datantype of args, not the args names.so when you returning the sum of x,y ,it returns zero.and the value passed by the main function goes nowhere.
your program must look like this
#include<iostream>
int x,y;
int sum(x,y)
{
return x+y;
}
int main()
{
int v,a,b;
cout<<"values of a and b";
cin>>a>>b;
v=sum(a,b)
cout<<"their sum is"<<v;
}
when you explicitly define the value in second case
i.e int x,y=0;
you are just explicitly giving the value of value y to 0 while the x implicitly remains 0 and since you are not giving the args name,the ultimately result return biy the function is zero,
Seems that you only need x and y inside your add function, so make them local to the function. There is no reason to make them global. Follow the "least accessibility" idiom to prevent other parts of your program from mistakenedly modifying variables.
You might need a global variable supposed you want to define a well known parameter that every function needs to know and yet modifiable during run time. If you want it fixed, then a global constant would be more proper.
Hope that helps.

cpp(15): error C2182: 'input' : illegal use of type 'void'

after making a fresh start on a new program i made for learning how arrays work in combinatio0n with void ive ran into the following problem.
cpp(15): error C2182: 'input' : illegal use of type 'void'
Does anyone know what causes this? I am new to the concept of void and array.
#include "stdafx.h"
#include <iostream>
using namespace std;
void input (int x );
int main()
{
int x = 0;
int a[ 5 ];
input ( a[ 5 ]);
{
void input(x);
for(int i = 1; i < 5; i++) {
cin >> a [ i ];
}
cin.get();
cout << a [ 3 ];
cin.get();
}
}
Your code has many problems with it. It's just not valid C++ as it is. Remember that C++, like any other programming language, is unforgiving when it comes to syntax. If it's not exactly the right syntax, it's not going to compile. You can't just write what you think makes sense. You need to learn the correct syntax and apply it.
It looks like you want everything from the for loop to the last cin.get() to be part of a function called input. To do that, you need to use the appropriate syntax for defining a function and you need to do it outside any other functions:
void input(int x) {
for(int i = 1; i < 5; i++) {
cin >> a [ i ];
}
cin.get();
cout << a [ 3 ];
cin.get();
}
This still has a problem though. The parameter type is int, yet it looks like you want to pass the entire array:
void input(int x[])
Note that this is not actually an array type parameter, but is really a pointer. When you pass an array to this function, x will be a pointer to its first element. The [] is just a convenient syntax.
Then, instead of passing a[5] to the function (which is an element that does not exist, since only a[0] to a[4] exist), you should be passing just a:
input(a);
You also loop from 1 to 4 - I'm not sure if this is intentional. If you want to input a value for each element of the array, you should be looping from 0 to 4.
You're going to have more errors after resolving the current one. Here's some quick pointers that may help. I don't want to just give you a solution because you are still learning and that won't help:
void as a keyword refers to the "nothing type" and is used in functions to denote having no return value
curly braces {} denote scope and can be used to define the body of a function, loop, or control statement
Functions themselves need to be declared and defined. The definition, or body of the function, can be later on in your code but the declaration needs to be present before you call it
Here's an example program to illustrate basic function parts:
#include <iostream>
// declaration
void Welcome();
int main()
{
// function call
Welcome(); // displays "Hello World"
return 0;
}
// definition
void Welcome()
{
std::cout << "Hello World" << std::endl;
}
More on functions
As far as arrays they are basically a contiguous block of memory large enough to hold a given amount of the same type. Here's a few things to remember about arrays:
They work with integral types as well as objects but are usually used for plain old data. e.g. int intArray[5]; is an array of 5 int types.
The index starts at 0 meaning intArray[0] from previous example is the first integer.
Using the array operator you can get and set values e.g. int last = intArray[4]; or intArray[0] = -1;
More on arrays
Check out the other answers for more on how to pass arrays as parameters but I also recommend picking a Good C++ Book ;-)

C++ Programming Error: expected unqualified-id before "{" token

I am a newbie at C++, and I am trying to make a "calculator" which: adds two numbers, subtracts two numbers, multiplies two numbers, divides two numbers, takes the sine of a number, takes the cosine of a number, or takes the tangent of a number. Here is the code:
#include <iostream>;
#include <cmath>;
#include <string>
int main ()
{}
int ask(std::string operation);
{
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
std::cin>>operation;
if (operation="Addition")
{
goto Add
}
float Add(float addend1, float addend2, float answer)
{
Add:
std::cout<<"Insert the first number to be added:\n";
std::cin>>addend1;
std::cout << "Insert the second number to be added:\n";
std::cin>>addend2;
answer=addend1+addend2;
std::cout<<addend1<<"+"<<addend2<<"="<<answer<<"\n";
break
}
}
There will be more functions later, but my problem is on line 7. There is an error that says: expected unqualified-id before "{" token. I know my indentation is horrible, but thanks!
You have a lot of issues in your code.
First, as Ivan points out, you are trying to define a function inside of a function (ask() inside main()). That isn't valid.
Second, you have a goto (why?!) attempting to jump to a label in another function. I doubt your compiler will even allow that, but how would you expect that to work? You are attempting to use variables passed to your function addition that don't exist as you never call the function and the stack has never been setup for it. This is bad, don't do it, just call the function properly.
Third, the #include preprocessor directive is terminated with a newline, not a semicolon. That could cause some (relatively) hard to track down compilation errors.
Fourth, you are mistakenly attempting to assign the const char* "Addition" to operation when what you meant to use was the equality operator ==. That won't work ether though because operation is an r-value and cannot be assigned to like that. If you want to modify it you will need to declare it as a pointer, but once again, that's not what you are going for semantically...
If you want to compare strings and (for whatever reason...) are intent on using pointers to char then you should be using strcmp. That said, you are in C++ land, so just use std:string instead.
Try something like this. I haven't enhanced your code in anyway, just made it something that will compile and run. I have made a few changes.
Aside from getting rid of a few syntax errors, your original Add function took the result as a float argument. Assigning to that from within the function would only modify a copy. You would need to take a pointer or reference if you want the caller to see the modified value, but you don't need that at all as you can simply return the result.
The string comparison is case sensitive, so you would probably want to change it to be case insensitive. I'm assuming no localization here :). I'm not performing error checking on the input either, so be aware that it may fail if the user enters something other than a valid floating point number.
#include <iostream>
#include <string>
using namespace std;
void Ask();
float Add( float, float );
int main( size_t argc, char* argv[] )
{
Ask();
return 0;
}
void Ask()
{
cout << "Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
string operation;
cin >> operation;
if( operation == "Addition" )
{
float first = 0, second = 0;
cout << "enter first operand";
cin >> first;
cout << "enter second operand";
cin >> second;
cout << "The result is: " << Add( first, second );
}
}
float Add( float first, float second )
{
return first + second;
}
ะก++ doesn't allow nested functions. You have function main() and trying to declare function ask() inside it. And compiler doesn't know what you want.
I commented your code a little bit, maybe that gets you started:
#include <iostream>;
#include <cmath>;
#include <string>;
int main () {
int ask (){ //you cannot nest functions in C++
char operation [20]; //why not use the string class if you include it anyway
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
std:cin>>operation;
if (operation="Addition"){ //you cannot compare char-strings in C++ like that
goto Addition; //don't use goto (I don't want to say "ever", but goto is only used in extremely rare cases) make a function call instead
}
}
float addition(float addend1, float addend2, float answer) //you probably want to declare the variables inside the function
{
Addition: //don't use labels
std::cout<<"Insert the first number to be added:\n";
std::cin>>addend1;
std::cout << "Insert the second number to be added:\n";
std::cin>>addend2;
answer=addend1+addend2;
std::cout<<addend1<<"+"<<addend2<<"="<<answer<<"\n";
}
Let's try to break this down..
You shouldn't use ; on the precompiler directives.
#include <iostream>;
#include <cmath>;
#include <string>;
Should be
#include <iostream>
#include <cmath>
#include <string>
.
int main () {
int ask (){
See Ivans answer for this
char operation [20];
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
std:cin>>operation;
if (operation="Addition"){
You can use std::string instead which is alot easier to deal with. Then you can write
#include <string>
...
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
std::string myString;
getline(cin, myString);
if (myString == "Addition"){
.
goto Addition;
}
}
float addition(float addend1, float addend2, float answer)
{
Not sure what is going on here.. but let's break Addition to it's own function
void Addition(){
// do addition here
}
.
Addition:
std::cout<<"Insert the first number to be added:\n";
std::cin>>addend1;
std::cout << "Insert the second number to be added:\n";
std::cin>>addend2;
answer=addend1+addend2;
std::cout<<addend1<<"+"<<addend2<<"="<<answer<<"\n";
}
Don't forget that you have to define the variables
int addend1;
int addend2;
int answer;
Hope this helps you along the way.
First int ask() what is that.Why do you start a block here.
Second you have two {s and three }s that's because of the ask().
I think that c++ does not support anonymus functions.
Third why do you use goto,when you have a function,just call the function.
Fourh your addition func should either be void or remove it's last parameter.
Also I think that you don't need string.h file unless you use some rather advanced funcs,the char array should be enough for your program.