Undefined reference to isPrime(int) but it is defined - c++

I am working on a practical assignment which requires us to use an external function to determine whether a integer entered is a prime number. I have created the header file, external functions file and main file but when i compile using
g++ -o main main.cpp extFunc.cpp
but I get the error:
/tmp/cca073oR.o: In function 'main':
main.cpp:(.text+0x42): undefined reference to 'isPrime(int)'
collect2: error: ld returned 1 exit status
The following are my cpp and header classes:
1)extFunc.h
bool isPrime(int num);
2)extFunc.cpp
# include "extFunc.h"
bool isPrime(int num) {
int i;
bool numPrime;
//Determine if number is prime
if (num <= 0) {
numPrime = false;
}
if (num = 1) {
numPrime = false;
}
if (num = 2) {
numPrime = true;
}
if (num = 3) {
numPrime = true;
}
else {
numPrime = true;
for(i = 2; i < num; i++) {
if ((num%i) == 0){
numPrime = false;
break;
}
}
}
//Return values
if (numPrime == true) {
return true;
} else {
return false;
}
}
3) main.cpp
#include <iostream>
#include "extFunc.h"
using namespace std;
int main() {
//Variables
int uNum;
bool prime;
cout << "Enter a number: ";
cin >> uNum;
prime = isPrime(uNum);
if (prime = true) {
cout << uNum << " is prime" << endl;
}
else {
cout << uNum << " is not prime" << endl;
}
return 0;
}
I have tried some of the other suggestions I was able to find on the site including using #ifndef in the header file but it did not fixed anything for me. I am unable to identify what possible could be the problem since the function in the header file and the function file is the same and it is called correctly (from what I can see) in the main file.

Thanks for all the responses. I managed to fix the issue. The university posted a guide to help us with the assignment this morning and I changed my makefile which fixed the issue.
This is what I had for my makefile
main: main.o extFunc.o
g++ -o main main.o extFunc.o
main.o: main.cpp extFunc.h
g++ -c main.cpp
extFunc.o: extFunc.h extFunc.cpp
g++ -c extFunc.cpp
run:
./main
clean:
rm *. main

I think you confuse between "=" and "=="
for instance
if (num = 1) {
[...]
}
should be :
if (num == 1) {
[...]
}
Perhapse that the use of "const" keyword is a good deal in order to avoid this kind of mistakes.
The error occurs :
on line 10, 13 and 16 in extFunc.cpp
and line 16 in main.cpp

Related

What is the problem with this boolean function?

I was wondering what I may have done wrong in writing this simple function which is supposed to return true if the given number is a prime, or false if not a prime.
bool isPrime(int num)
{
if (num <= 1)
{
status = false;
}
else
{
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
{
dividers++;
}
}
if (dividers == 2)
{
status = true;
}
else
{
status = false;
}
}
return status;
}
Obviously, my main looks like this:
bool isPrime(int num);
bool status;
int dividers = 0;
int main() {
isPrime(2);
if (!isPrime)
{
std::cout << "Not prime" << std::endl;
}
else
{
std::cout << "Prime" << std::endl;
}
return 0;
}
I'm a C++ beginner and I'd really appreciate it if someone could help me there and correct my logic.
Have a good day:)
The immediate problem is in this two lines:
isPrime(2);
if (!isPrime)
The first line calls the function and discards the returned value. The second line converts a pointer to the function to bool. The output of your code does not depend on what you actually do in isPrime.
That is not how you call a function and use its result!
Instead you want
if (isPrime(2)) {
or
bool isP = isPrime(2);
if (isP) { ...
As mentioned in comments, there are also problems in the implementation of isPrime, but I hope this is enough to set you back on the right track.
PS: You should get rid of the global variable status. You do not need both, the return value and a global that stores the result, and if you can choose, you should definitely go for the return value.

list requires class type

So I'm trying to make a bubble sort algorithm in class and I'm having this problem where it keeps giving me an error when I'm trying to find the length of the list where it says "expression must have a class type" and for the life of me I cannot figure out what to do. the tutorial I'm using isn't an help and I cannot find any other people with the same problem.
if anyone gets what it is asking I would appreciate the help, and any explanation would also be appreciated as I'm still new and would like to understand so I can try to learn
this was all done on VS 2017 (the free version)
#include "pch.h"
#include <iostream>
using std::cout;
using std::endl;
int main()
{
bool found = true;
int target{ 0 };
int temp{};
bool ordered{ false };
int list[10] = { 4,6,5,1,3,2,10,8,9,7 };
cout << list.length() << endl;
bool swapped{ false };
while (ordered = false)
{
target = 0;
while (target != list.length)
{
if (list[target] > list[target + 1])
{
swapped == true;
list[target] = temp;
list[target] = list[target + 1];
list[target + 1] = temp;
target = target + 1;
}
else
{
target = target + 1;
}
}
if (swapped == false)
{
ordered = true;
}
}
cout << list << endl;
getchar();
return 0;
}
link to the photo of the error message
The error you have mentioned ("expression must have a class type") is caused by the below statement and other similar statements :
cout << list.length() << endl;
list is an integer array of size 10 as per this statement int list[10];
So you cannot use a . on it. You can use the . operator on a structure or class or union only. And even if list were a class/structure, length() method should be defined in it for the above to work.
Instead you should use sizeof operator. You can store it in a variable and use it later on.
size_t length = sizeof list/sizeof list[0];
cout << length << endl;

Variable not declared within this scope Array Linear Search

I am trying to make a program in C++ that will search for a desired value in an array of size 10 using a separate search function. Below is the code:
main.cpp
#include <iostream>
#include <array>
using namespace std;
int main()
{
cout << "Welcome to the array linked list program.";
int sanadA[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int d = 0;
cin >> d;
while (d =! 0)
{
cout << "Number to be found";
cin >> d;
bool found = seqSearch1(sanadA, 10, d, -1);
cout << found;
}
}
seqSearch1.cpp
#include <iostream>
using namespace std;
bool jw_search (int *list, int size, int key, int*& rec)
{ //Basic sequential search.
bool found = false;
int i;
for(i=0;i<size;i++)
{
if (key == list[i])
{
break;
}
if (i < size)
{
found = true;
rec = &list[i];
}
}
return found;
}
I get the errors:
C:\Users\tevin\Documents\sanad\main.cpp|13|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\bits\c++0x_warning.h|32|error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.|
C:\Users\tevin\Documents\sanad\main.cpp|19|error: 'seqSearch1' was not declared in this scope|
I need help figuring why this happens.
I assume that the error occurs on this line:
bool found = seqSearch1(sanadA, 10, d, -1);
The problem is that you have not declared any function named seqSearch1(). Instead you have a function named jw_search(). So you can change the line to this:
bool found = jw_search(sanadA, 10, d, -1);
But you also need a header file called seqSearch1.h with the following line:
bool jw_search (int *list, int size, int key, int*& rec);
And finally add this line to the top of main.cpp:
#include "seqSearch1.h"
When you compile your code, you will need to include all source files in the command. For example, if you are using g++, you can do something like this:
g++ main.cpp seqSearch1.cpp
To understand how this works, you need to learn about header files and the difference between a function declaration and a function definition. You should also learn about the difference between the compiler and the linker.
Code-Apprentice has the direct answer to your question. If you want the code in multiple files then a declaration of the seqSearch1 function will need to be main.cpp or included via #include directive
The code has multiple problems. I've fixed it up a bit for you and put it in a single file.
#include <iostream>
#include <array>
using namespace std;
bool seqSearch1 (int *list, int size, int key, int& rec)
{//Basic sequential search.
bool found = false;
int i;
for(i=0;i<size;i++)
{
if (key == list[i])
{
found = true;
rec = i;
break;
}
}
return found;
}
int main()
{
cout << "Welcome to the array linked list program." << endl;
int sanadA[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int d = -1;
while (d != 0)
{
cout << "Number to be found, 0 to end?";
cin >> d;
if(d == 0) break;
int index = -1;
bool found = seqSearch1(sanadA, 10, d, index);
if(found) cout << "Found" << endl;
else cout << "Not Found" << endl;
}
}
Several issues:
The function was referred to by the wrong name.
The loop structure was a confused.
The fourth argument to seqSearch1 had type confusion.

Code Gives No Errors but Does Nothing in Command Prompt

I am having trouble getting my code to run on command prompt, I am getting no errors but when I run the code nothing happens.
#include <iostream>
#include <stdexcept>
using namespace std;
//defines the maximum queue size
#define MAX_QUE_SIZE 10
//creates the "rules" for the queue
class queue {
private:
int A[MAX_QUE_SIZE];
int front;
int rear;
public:
queue() {
front = -1;
rear = -1;
}
//checks to see if the queue is empty
bool isEmpty() {
return (front == -1 && rear == -1);
}
//checks to see if the queue if full
bool isFull() {
return (rear + 1) % MAX_QUE_SIZE == front ? true : false;
}
//checks to see if the queue is full, if not then it adds to the queue.
//if so it gives an error message.
void enqueue(int element) {
if (isFull()) {
throw std::overflow_error("QUEUE FULL");
}
if (isEmpty()) {
front = 0;
rear = 0;
}
else {
rear = (rear + 1) % MAX_QUE_SIZE;
}
A[rear] = element;
}
//checks to see if the queue is empty, if not then it deletes from the queue
//if sos it gives an error message.
void dequeue() {
if (isEmpty()) {
throw std::underflow_error("QUEUE EMPTY");
}
else if (front == rear) {
rear = -1;
front = -1;
}
else {
front = (front + 1) % MAX_QUE_SIZE;
}
}
//checks to see if the queue is empty, if so it gives a message saying so
//if not then it prints all the items in the queue
void printqueue() {
if (isEmpty()) {
cout << "EMPTY QUEUE";
}
else {
int count = (rear + MAX_QUE_SIZE - front) % MAX_QUE_SIZE + 1;
cout << "Queue : ";
for (int i = 0; i < count; i++) {
int index = (front + i) % MAX_QUE_SIZE;
cout << A[index] << " ";
}
cout << "\n\n";
}
}
};
int main()
{
queue Q; // creating an instance of Queue.
int i;
int k = 0;
int x;
std::cout << "Please enter some integers (enter 0 to exit):\n";
//a do-while statement that adds to the queue
do {
std::cin >> i;
//tries to add to the queue, if the queue is full it gives and overflow error
try {
Q.enqueue(i);
}
catch (std::overflow_error e) {
std::cout << e.what() << endl;
}
} while (i != 0);
std::cout << endl;
Q.printqueue();
std:cout << "How many values do you want to dequeue:\n";
std::cin >> x;
cout << endl;
//a for loop that dequeues the number of items the user wants to delete
//try the foor loop and dequeue function, if the queue is empty then it gives an underflow error
try {
for (int k = 0; k < x; k++) {
Q.dequeue();
}
}
catch (std::underflow_error e) {
std::cout << e.what() << endl;
}
Q.printqueue();
return 0;
}
I am also typing in g++ -o ehQue ehQue.cpp to compile it. I am not sure if this is causing the error or if my code itself is causing the error. Any amount of help will be appreciated.
I suspect you're just not executing your code. It compiles and runs.
You're compiling the program (not executing it) with:
g++ -o ehQue ehQue.cpp
The command can be understood as calling the program "g++" which should be just an alias to "gcc" which is the compiler. It takes sources code and produces object code, which is then linked to produce an executable binary (program.)
-o ehQue
Is the command parameter to specify the output file name. The compiler will take the provided files and (attempt to) produce a working executable called "ehQue".
ehQue.cpp
Is your source code, which you specified to the compiler.
Within your terminal (where you typed the g++ command) you will, also, need to call the program using a command such as:
./ehQue
Or to be specific to the Windows command prompt:
ehQue
Where you should find that your program runs.
(Tangential) Unless you specifically need to re-invent the wheel, one of CPP's defining features is the Standard Template Library (STL) which is part of the core specification... wrapping a std::deque in a class with your print functions would be advisable.

Writing automated tests for unix/linux

This is a constraint based question. I am using my school's server to SSH into. I have created a binary search program in C++. Here's the code:
#include <iostream>
using namespace std;
template<typename T>
bool bsearch(T num)
{
T arr[] = {5.3, 6.62, 7.74, 10.22, 13.22};
int len = (sizeof(arr)/sizeof(*arr));
int mid, l_bound=0, u_bound = len-1;
while (l_bound <= u_bound)
{
mid =(l_bound+u_bound)/2;
if (num > arr[mid])
l_bound = mid+1;
else if (num < arr[mid])
u_bound = mid -1;
else
return true;
}
return false;
}
int main()
{
float num;
cout <<"Number to search: ";
cin >>num;
if (bsearch(num) == true)
cout <<"Number found!\n";
else
cout <<"Nubmer not found!\n";
}
It's pretty simple. The only thing is: I want to write some unit tests for this, but I'm having problems getting any of the libraries on my schools SSH server. Is there another way to do this? When I try to include gtest and write this:
#include <gtest/gtest.h>
ASSERT_NE(1, 1);
It just gives me the error that
error: expected unqualified-id before 'switch'
error: expected unqualified-id before 'else'"
Is there a standard library for C++ that I can use? Or how could I get Google's test suite working on this?