The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Linker error???
Discuss Linker error??? in the C Programming forum on Dev Shed. Linker error??? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 2nd, 2013, 06:16 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 2 h 16 m 35 sec
Reputation Power: 0
|
|
|
Linker error???
i am building a program that finds the longest path to calculate the solution...
this is the code:
#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define _GNU_SOURCE
int startIndex = 0;
int countOneBranchLength(int** mtrx, int rowcount, int curRow){
int res = 1;
int i = startIndex;
while (i < rowcount) {
if (mtrx[i][1] == mtrx[curRow][2]){
int nodesFollowing = mtrx[i][4];
if ( nodesFollowing > 0) {
int presStartIndex = startIndex;
startIndex = 0;
res += countLength(mtrx, rowcount, i, nodesFollowing) -2;
startIndex = presStartIndex;
}
startIndex = i+1;
}
i++;
}
return res + 1;
}
int countLength(int** mtrx, int rowcount, int curRow, int nodesFound){
int l = 0;
int lt = 0;
int j = 0;
while (j < nodesFound) {
lt = countOneBranchLength(mtrx,rowcount,curRow) + 1;
if (lt > l)
l = lt;
j++;
}
return l;
}
int main(int argc, const char * argv[])
{
int m,k,i,j,s1,s2,res = 0,currlength;
FILE *fp,*fp2;
fp=fopen("scidinner.in","r");
res = 0;
fscanf(fp,"%d %d",&m,&k);
int** mtrx;
mtrx=(int**)malloc(k*sizeof(int*));
for (i=0; i<k; i++)
mtrx[i]=(int*)malloc(4*sizeof(int));
i = 0;
while(!feof(fp) && i < k)
{
fscanf(fp,"%d %d",&s1,&s2);
mtrx[i][1] = s1;
mtrx[i][2] = s2;
mtrx[i][3] = -1;
mtrx[i][4] = 0;
i++;
}
fclose(fp);
i=0;
while (i < k) {
s2 = mtrx[i][2];
j=0;
while (j < k) {
if (mtrx[j][1] == s2){
mtrx[j][3] = i;
mtrx[i][4]++;
}
j++;
}
i++;
}
i=0;
while (i < k) {
if (mtrx[i][4] > 0){
startIndex = 0;
currlength = countLength(mtrx, k, i, mtrx[i][4]) ;
if (currlength > res)
res = currlength;
}
i++;
}
fp2=fopen("scidinner.out","w");
fprintf(fp2,"%d", res);
fclose(fp2);
for (i=0; i<k; i++)
free((void*)mtrx[i]);
free((void*)mtrx);
return res;
}
But my debug is giving me these error reports:
In function `main':
[Linker error] undefined reference to `countLength'
ld returned 1 exit status
and I can't find my error...Please Heeelp!!!
|

February 2nd, 2013, 06:28 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Code not formatted, rendering it unreadable. So I didn't bother to even try to read it. Nor will anybody else want to bother.
This was your sixth post here, so you have been told this already: use code tags! Here is what it looks like when you do; compare this with your opening post and see the difference.
Code:
#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define _GNU_SOURCE
int startIndex = 0;
int countOneBranchLength(int** mtrx, int rowcount, int curRow){
int res = 1;
int i = startIndex;
while (i < rowcount) {
if (mtrx[i][1] == mtrx[curRow][2]){
int nodesFollowing = mtrx[i][4];
if ( nodesFollowing > 0) {
int presStartIndex = startIndex;
startIndex = 0;
res += countLength(mtrx, rowcount, i, nodesFollowing) -2;
startIndex = presStartIndex;
}
startIndex = i+1;
}
i++;
}
return res + 1;
}
int countLength(int** mtrx, int rowcount, int curRow, int nodesFound){
int l = 0;
int lt = 0;
int j = 0;
while (j < nodesFound) {
lt = countOneBranchLength(mtrx,rowcount,curRow) + 1;
if (lt > l)
l = lt;
j++;
}
return l;
}
int main(int argc, const char * argv[])
{
int m,k,i,j,s1,s2,res = 0,currlength;
FILE *fp,*fp2;
fp=fopen("scidinner.in","r");
res = 0;
fscanf(fp,"%d %d",&m,&k);
int** mtrx;
mtrx=(int**)malloc(k*sizeof(int*));
for (i=0; i<k; i++)
mtrx[i]=(int*)malloc(4*sizeof(int));
i = 0;
while(!feof(fp) && i < k)
{
fscanf(fp,"%d %d",&s1,&s2);
mtrx[i][1] = s1;
mtrx[i][2] = s2;
mtrx[i][3] = -1;
mtrx[i][4] = 0;
i++;
}
fclose(fp);
i=0;
while (i < k) {
s2 = mtrx[i][2];
j=0;
while (j < k) {
if (mtrx[j][1] == s2){
mtrx[j][3] = i;
mtrx[i][4]++;
}
j++;
}
i++;
}
i=0;
while (i < k) {
if (mtrx[i][4] > 0){
startIndex = 0;
currlength = countLength(mtrx, k, i, mtrx[i][4]) ;
if (currlength > res)
res = currlength;
}
i++;
}
fp2=fopen("scidinner.out","w");
fprintf(fp2,"%d", res);
fclose(fp2);
for (i=0; i<k; i++)
free((void*)mtrx[i]);
free((void*)mtrx);
return res;
}
In the first function, countOneBranchLength, you call another function, countLength, which the compiler doesn't know anything about. countLength shows up later, but that doesn't do any good. You need to add a function prototype for countLength that is placed above countOneBranchLength.
Normally, this kind of mistake does not result in linker error, but rather in a "undefined reference" compiler warning.
Also, if this a C++, then because of function overloading you may need to check that you always give it the right argument types.
|

February 3rd, 2013, 03:28 AM
|
 |
Contributed User
|
|
|
|
Code:
mtrx[i][1] = s1;
mtrx[i][2] = s2;
mtrx[i][3] = -1;
mtrx[i][4] = 0;
As far as I can tell, every single reference to the minor dimension of this matrix is 1-based rather than 0-based.
So EVERY reference to mrtx[x][4] (really, what did you save by not saying matrix everywhere) is an out of bounds access.
> int m,k,i,j,s1,s2,res = 0,currlength;
Use meaningful variable names.
i,j are fine, when they're just the loop control variables.
But m,k - what are those?
How about something like numRows and numCols.
As well as being readable, it's a lot harder to make a mistake (and a lot easier to spot a mistake) than a scattering of single letter identifiers.
A single letter variable takes only ONE key press to make the mistake, and the resulting bug is almost invisible to spot.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|